2012-07-11 80 views
4

在相对布局中观察比较奇怪的行为。EditText无端调整大小

这是初始状态: enter image description here

定义为:

<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"); 

和变成这样:
enter image description here

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

+0

我试图重现您的问题。复制/粘贴您发布的代码,在Cupcake模拟器上运行该代码,但我没有发现任何问题,editText框保持相同的高度。你使用的是什么版本的Android?你在做其他任何事情来自定义EditText框吗?你使用任何特定的主题? – Matthieu 2012-07-14 23:47:12

+0

为什么你不使用所有3个元素的重量(编辑文本,文本视图,按钮),并把它们放入一个有一定权重的水平线性布局? – 2012-07-16 08:40:32

回答

1

bleedCount的EditText大小调整是由于你的提示文本变得比一行时(pos == 1)长。

如果您在代码中注释掉以下行,调整大小将阻止事情发生:

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

也许你可以make it shorter/smaller防止调整大小?

+0

就是这样,谢谢。 – 2012-07-16 18:33:39

2

它与android:ems="10"
连接。当改变的EditText它的宽度,示出的视图了x12后,它必须被splited成两行。

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

我认为你不需要ems
设置的EditText单内衬:android:singleLine="true"

+0

删除了'ems'并添加了'singleLine'。仍然看到相同的行为。 – 2012-07-11 20:07:38

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 
} 
} 

希望它可以帮助...