如何让 EditText 根据框中的文本进行拉伸?

How do you get an EditText to stretch based on the text in the box?

我有一个自定义日期选择器(不使用 DatePickers 因为它们不适合小屏幕)。月、日和年都是称为 NumberPickers 的自定义视图。

问题是我需要根据编辑文本中的文本拉伸日期选择器上的按钮和拉伸编辑文本。数字选择器都是以编程方式添加的。

号码选择器布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutNumberPicker"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@null"
android:orientation="vertical" >

<com.mycompany.ediary.NumberPickerButton
    android:id="@+id/increment"
    android:layout_width="wrap_content"
    android:layout_height="42dp"
    android:background="@null"
    android:scaleType="fitXY"
    android:layout_weight="1"
    android:src="@drawable/timepicker_up_btn" />

<EditText
    android:id="@+id/timepicker_input"
    style="?android:attr/textAppearanceMediumInverse"
    android:layout_width="wrap_content"
    android:layout_height="49dp"
    android:background="@drawable/timepicker_input"
    android:clickable="false"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:gravity="center"
    android:singleLine="true"
    android:layout_weight="1"
    android:scaleType="fitXY"
    android:textSize="16dp" />

<com.mycompany.ediary.NumberPickerButton
    android:id="@+id/decrement"
    android:layout_width="wrap_content"
    android:layout_height="41dp"
    android:background="@null"
    android:scaleType="fitXY"
    android:layout_weight="1"
    android:src="@drawable/timepicker_down_btn" />

但是,EditText 的宽度都相同,而不是根据文本的大小进行拉伸。如您所见,年份被截断了。我不确定如何让它伸展。当我将 EditText 的宽度设置为设定大小而不是 wrap_content 时,按钮不会拉伸到相同的宽度(它们是 9 个补丁)。

显示年月日的代码是:

LinearLayout layoutWidgets = new LinearLayout(con);
    layoutWidgets.setId(GlobalVars.getLatestId());
    /*****Set width of Date******/
    layoutWidgets.setMinimumWidth(185);
    layoutWidgets.setOrientation(LinearLayout.HORIZONTAL);
    /*****Set width of Date******/
    params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    if (GlobalVars.subjectLeftToRight == 1) {
        params.addRule(RelativeLayout.BELOW, btnDTPicker.getId());
        params.addRule(RelativeLayout.ALIGN_LEFT, lblQuestionText.getId());
        params.setMargins(0, 10, 0, 0);
        //params.setMargins(0, 0, 0, 0);

    } else {
        params.addRule(RelativeLayout.BELOW, btnDTPicker.getId());
        params.addRule(RelativeLayout.ALIGN_RIGHT, lblQuestionText.getId());
        params.setMargins(0, 10, 0, 0);
        //params.setMargins(0, 0, 0, 0);
    }

    layoutWidgets.setWeightSum(1);
    layoutWidgets.setLayoutParams(params);

月份:

layoutMonth = new LinearLayout(con);
    layoutMonth.setId(GlobalVars.getLatestId());
    layoutMonth.setOrientation(LinearLayout.VERTICAL);
    layoutMonth.setMinimumWidth(95);
    pickerMonth = new NumberPicker(con);
    pickerMonth.setId(GlobalVars.getLatestId());
    pickerMonth.setupText(147);// [Blank]
    pickerMonth.setTag(tagMonthPicker);
    pickerMonth.setVisibility(View.GONE);
    pickerMonth.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
    pickerMonth.setRange(1, 12, GlobalVars.calNamesShort);

LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    lparams.weight = .3f;
    pickerMonth.setLayoutParams(lparams);
    layoutMonth.addView(pickerMonth);

日期:

layoutDay = new LinearLayout(con);
    layoutDay.setId(GlobalVars.getLatestId());
    layoutDay.setTag(tagDayLayout);
    layoutDay.setOrientation(LinearLayout.VERTICAL);
    layoutDay.setMinimumWidth(95);
    layoutDay.setVisibility(View.GONE);

    pickerDay = new NumberPicker(con);
    pickerDay.setId(GlobalVars.getLatestId());
    pickerDay.setupText(147);// [Blank]
    pickerDay.setTag(tagDayPicker);
    pickerDay.setVisibility(View.GONE);
    pickerDay.setFormatter(NumberPicker.TWO_DIGIT_FORMATTER);
        lparams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lparams.weight = .3f;
    pickerDay.setLayoutParams(lparams);
    layoutDay.addView(pickerDay);

年份:

layoutYear = new LinearLayout(con);
    layoutYear.setId(GlobalVars.getLatestId());
    layoutYear.setTag(tagYearLayout);
    layoutYear.setMinimumWidth(95);
    layoutYear.setOrientation(LinearLayout.VERTICAL);
    layoutYear.setVisibility(View.GONE);

    pickerYear = new NumberPicker(con);
    pickerYear.setId(GlobalVars.getLatestId());
    pickerYear.setupText(147);// [Blank]
    pickerYear.setTag(tagYearPicker);
    pickerYear.setVisibility(View.INVISIBLE);
    pickerYear.setRange(minYear, maxYear);

        params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    /** width of year **/
    lparams.weight = .4f;
    params.setMargins(0, 0, 0, 0);
    pickerYear.setLayoutParams(params);
    layoutYear.addView(pickerYear);

将视图添加到布局:

layoutWidgets.addView(layoutMonth);
layoutWidgets.addView(layoutDay);
layoutWidgets.addView(layoutYear);

编辑

public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs);

    mContext = context;

    setOrientation(VERTICAL);
    LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.number_picker, this, true);
    mHandler = new Handler();
    InputFilter inputFilter = new NumberPickerInputFilter();
    mNumberInputFilter = new NumberRangeKeyListener();
    mIncrementButton = (NumberPickerButton)findViewById(R.id.increment);
    mIncrementButton.setOnClickListener(this);
    mIncrementButton.setOnLongClickListener(this);
    mIncrementButton.setNumberPicker(this);
    mDecrementButton = (NumberPickerButton)findViewById(R.id.decrement);
    mDecrementButton.setOnClickListener(this);
    mDecrementButton.setOnLongClickListener(this);
    mDecrementButton.setNumberPicker(this);

    mText = (EditText)findViewById(R.id.timepicker_input);
    /*** set size of number picker **/
    //mText.setWidth(Math.round(convertFromDp(80)));
    mText.setOnFocusChangeListener(this);
    mText.setFilters(new InputFilter[] { inputFilter });
    mText.setRawInputType(InputType.TYPE_CLASS_NUMBER);


    if (!isEnabled()) {
        setEnabled(false);
    }
}

public float convertFromDp(int input) {
    final float scale = getResources().getDisplayMetrics().density;
    return ((input - 0.5f) / scale);
}

我的猜测是父线性布局覆盖了大小。我建议您不要使用 wrap_content,而是使用特定的 dp 并将重力设置为中心(或 center_horizontal),以便您的 UI 在外观和感觉上保持一致。假设你解决了这个问题,如果你让它绕行,这意味着按钮会四处移动!

要解决这个问题,你可以 post 父线性布局的布局参数吗?

编辑:再次检查您的代码后

所以回到你原来的问题。查看单个日期选择器的父项,您有三个垂直方向的子项 NumberButtonEditTextNumberButton。将父级的宽度设置为 wrap_content,将 NumberButton 的宽度设置为 match_parent,并将 EditText 的宽度设置为 wrap_content。这现在应该使 EditText 成为宽度的控制因素,NumberButtons 扩展到任何宽度,前提是没有对父级的宽度施加约束(没有足够的 space 可用,显式大小集)。例如,如果您对设置为水平的父线性布局中的一组执行此操作,这将起作用。如果不是在这一点上,那么这是父布局的问题,而不是这些布局本身的问题。