4

在相对布局中观察到相当奇怪的行为。

这是初始状态: 在此处输入图像描述

定义为:

<EditText
    android:id="@+id/bleedCount"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/abrMult"
    android:layout_below="@+id/textView4"
    android:layout_marginRight="10dp"
    android:ems="10"
    android:inputType="number"
    >
    <requestFocus />
</EditText>

<TextView
    android:id="@+id/abrMult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/abrSubmit"
    android:layout_alignBaseline="@+id/abrSubmit"
    android:layout_marginRight="10dp"
    android:text="x12" />

<Button
    android:id="@+id/abrSubmit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView4"
    android:layout_alignParentRight="true"
    android:text="Calculate" />

onItemSelected下拉列表中,它正在像这样更改:

abrSubmit.setText(pos == 1 ? "Calculate" : "Submit");
abrMult.setVisibility(pos == 1 ? View.VISIBLE : View.GONE);
bleedCount.setHint(pos == 1 ? "# of Meow/month" : "# of Meow/year");

变成了这样:
在此处输入图像描述

请注意 EditTextbleedCount在第二张图片上的高度。的值bleedCount.getHeight()从 72 变为 95,我不明白是什么原因造成的。

4

3 回答 3

2

它与 当 EditText 改变它的宽度时,用 显示视图后,它必须被分成两行。android:ems="10"
x12

ems给定字体的大小为一个字母。

我觉得你不需要ems
将 EditText 设置为单行:android:singleLine="true"

于 2012-07-11T19:50:35.450 回答
1

The bleedCount EditText resizing is due to your hint text becoming longer than a single line when (pos == 1).

If you comment out the following line in your code, the resizing will stop happening:

// bleedCount.setHint(pos == 1 ? "# of Meow/month" : "# of Meow/year");

Maybe you can make it shorter/smaller to prevent the resizing?

于 2012-07-15T01:14:55.657 回答
0

好的,因为我没有你的整个代码库,所以我把一些简单的东西放在一起来复制你正在做的事情。仅供参考,我使用的是 ICS 4.1。我没有遇到您遇到的任何问题,所以这可能是 API 问题。也许你可以看看我的代码库,看看它和你自己的代码库有什么不同。其中可能存在解决方案。

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_rl"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Top TextView" />

<Spinner
    android:layout_below="@+id/textView4"
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:layout_below="@+id/spinner1"
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Above EditText" />

<EditText
    android:id="@+id/bleedCount"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView5"
    android:layout_marginRight="10dp"
    android:layout_toLeftOf="@+id/abrMult"
    android:ems="10"
    android:inputType="number" >
</EditText>

<TextView
    android:id="@+id/abrMult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView5"
    android:layout_alignBaseline="@+id/abrSubmit"
    android:layout_marginRight="10dp"
    android:layout_toLeftOf="@+id/abrSubmit"
    android:text="x12"
    android:visibility="gone" />

<Button
    android:id="@+id/abrSubmit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView5"
    android:text="Submit" />

</RelativeLayout>

代码:

public class ExampleActivity extends Activity implements OnItemSelectedListener {

private Button submitButton;
private TextView tv;
private Spinner spinner;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_example);

    submitButton = (Button) findViewById(R.id.abrSubmit);
    tv = (TextView) findViewById(R.id.abrMult);
    spinner = (Spinner) findViewById(R.id.spinner1);

    // create the data array for the spinner
    String[] strings = { "This", "That", "The Other" };

    // create the spinner adapter
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, strings);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // set the adapter on the spinner
    spinner.setAdapter(adapter);

    // set the event listener for the spinner
    spinner.setOnItemSelectedListener(this);
}

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    if (submitButton.getText().equals("Calculate")) {
        submitButton.setText("Submit");
        tv.setVisibility(View.GONE);
    } else {
        submitButton.setText("Calculate");
        tv.setVisibility(View.VISIBLE);
    }

}

public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
}
}

希望能帮助到你...

于 2012-07-14T23:41:53.303 回答