我正在尝试以编程方式调整EditText
手机方向变化时的大小,这就是我目前所拥有的:
private final float editTextWidthLandscap = 380;
private final float editTextWidthPortrait = 220;
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Resources r = getResources();
float px;
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editTextWidthLandscap, r.getDisplayMetrics());
input.setWidth((int) px); // input is an EditText
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editTextWidthPortrait, r.getDisplayMetrics());
input.setWidth((int) px); // input is an EditText
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
我的清单文件中也有这个(以防万一):
<activity
android:name=".Results"
android:configChanges="keyboardHidden|orientation"
android:theme="@android:style/Theme.NoTitleBar"
android:windowSoftInputMode="stateHidden" >
</activity>
奇怪的是它显示的Toast
很好,但似乎忽略了该input.setWidth()
声明。如果有人能解释为什么EditText
没有调整大小和/或给我指点我如何能够正确地做到这一点,将不胜感激。
编辑:
这是 EditText 所在的 XML 文件的一部分
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="@color/purple"
android:gravity="center_horizontal|center_vertical"
android:orientation="horizontal" >
<EditText
android:id="@+id/input"
android:layout_width="220dp"
android:layout_height="50dp"
android:background="@drawable/search_bar"
android:hint="@string/input_hint"
android:inputType="text|textNoSuggestions"
android:maxLines="1"
android:minHeight="50dp"
android:minWidth="220dp"
android:selectAllOnFocus="true"
android:singleLine="true"
android:textColor="@color/black_grey"
android:textSize="16.67sp" />
<Button
android:id="@+id/search_button"
android:layout_width="65dp"
android:layout_height="50dp"
android:background="@drawable/search_button" />
</LinearLayout>
编辑2:(解决方案)
我仍然不知道究竟input.setWidth()
是什么,但我发现我必须使用LayoutParams
这个问题。下面是现在为我工作的代码:
private final float editTextWidthLandscap = 380;
private final float editTextWidthPortrait = 220;
private final float editTextHeight = 50;
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Resources r = getResources();
float widthpx;
float heightpx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editTextHeight, r.getDisplayMetrics());;
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
widthpx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editTextWidthLandscap, r.getDisplayMetrics());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) Math.round(widthpx), (int) Math.round(heightpx));
trademarkTextfield.setLayoutParams(lp);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
widthpx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editTextWidthPortrait, r.getDisplayMetrics());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) Math.round(widthpx), (int) Math.round(heightpx));
trademarkTextfield.setLayoutParams(lp);
}
}