Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何根据框中的文本获取要拉伸的EditText?_Android_Android Layout_Android Edittext - Fatal编程技术网

Android 如何根据框中的文本获取要拉伸的EditText?

Android 如何根据框中的文本获取要拉伸的EditText?,android,android-layout,android-edittext,Android,Android Layout,Android Edittext,我有一个自定义日期选择器(不使用日期选择器,因为它们不适合小屏幕)。月、日和年都是称为NumberPickers的自定义视图 问题是我需要日期选择器上的按钮进行拉伸,而编辑文本则需要根据编辑文本中的文本进行拉伸。数字选择器都是通过编程方式添加的 数字选择器布局: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layoutNumberPicker" andro

我有一个自定义日期选择器(不使用
日期选择器
,因为它们不适合小屏幕)。月、日和年都是称为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" />
当月:

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_内容,而是使用特定的dp并将重心设置为中心(或水平居中),以便您的UI在外观上保持一致。假设你解决了这个问题,如果你让它包起来,这意味着按钮会四处移动

要解决此问题,您可以发布父线性布局的布局参数吗

编辑:再次查看代码后


回到你原来的问题。查看单个日期选择器的父项,您有三个垂直方向的子项
NumberButton
EditText
NumberButton
。将父项的宽度设置为
wrap\u content
,将
NumberButton
s的宽度设置为
match\u parent
,将
EditText
的宽度设置为
wrap\u content
。这将使
EditText
成为宽度的控制因素,
NumberButtons
扩展到任意宽度,前提是没有对父项的宽度施加约束(没有足够的可用空间,显式设置大小)。例如,如果对设置为“水平”的父线性布局中的一个集执行此操作,则此操作将有效。如果不是在这一点上,那么是父布局的问题,而不是这些布局本身的问题

@DerGolem你指的是本地的日期采集者吗?@DerGolem这不是一个选项。或者你可以简单地使用两位数的年份。一千年后就不会再有千年虫了…@DerGolum-再说一次,别无选择。你拥有所有的布局。没有父级的布局参数。啊,在这种情况下,您的父级线性布局都是
fill\u parent
,这意味着它们将尽可能地拉伸。尝试在其父对象上设置“匹配父对象”,或设置硬编码值,然后查看这是否会更改编辑文本宽度。我也不确定什么是
pickerMonth=newnumberpicker(con)引用到--这是哪个视图?让layoutWidgets的布局宽度与\u父对象匹配会清除时间选择器。为什么要对LinearLayout参数使用RelativeLayout规则?您是否尝试使用适合所有子项的硬编码值?是否有xml作为所有子项的父项?
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);
}