我正在尝试在手机方向发生变化时以编程方式调整 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();
}
}
我的 list 文件中也有这个(以防万一):
<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);
}
}
最佳答案
根据我自己的经验,有时我们倾向于过度思考不良行为的原因。我不止一次发现,我认为理所当然的事情并非如此。
您应该检查这两种情况的 px 参数是否真的不同。我知道这是一个基础知识,但也许 applyDimension
没有按照您预期的方式工作,或者在其他地方定义了一些最小宽度,您只是点击它。
您还应该在调用 setWidth()
之前和之后调用 getWidth()
。为什么?因为也许该功能正在运行,而系统确实认为宽度发生了变化,而问题出在其他地方。
关于android - 在方向更改时以编程方式调整 EditText 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10688465/