问题
I'm trying to programmatically resize an EditText
when the orientation of the phone changes, here's what I have so far:
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();
}
}
I also have this in my manifest file (in case it matters):
<activity
android:name=".Results"
android:configChanges="keyboardHidden|orientation"
android:theme="@android:style/Theme.NoTitleBar"
android:windowSoftInputMode="stateHidden" >
</activity>
The strange thing is that it displays the Toast
just fine but it seems to ignore the input.setWidth()
statement. If anyone could explain why the EditText
is not being resized and/or give me pointers as to how i would be able to this correctly it would be greatly appreciated.
Edit:
This is a part of the XML file in which the EditText resides
<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>
Edit 2: (solution)
I still don't know what input.setWidth()
does exactly but i found out i had to use LayoutParams
for this issue. Below is the code that is working for me now:
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);
}
}
回答1:
Based on my own experience, sometimes we tend to overthink the reason for a bad behavior. I found more than once that things that I took for granted, were not so.
You should check that the px argument for the two cases are really different. I know this is a basics, but maybe applyDimension
is not working the way you expect or there is some minimum width defined elsewhere and you just hit it.
You should also call getWidth()
before and after the call to setWidth()
. Why? because maybe the function is working and the system does think the width haschanged and the problem lies elsewhere.
回答2:
I'm not exactly sure why the size isnt changing programmatically, however, you may instead want to use a different layout file for landscape mode. Just create a new folder called 'layout-land' and put your layout.xml file in there (with the same ids and filename) and change the sizes accordingly.
This may not be this best solution depending on your case, but its just another option...
来源:https://stackoverflow.com/questions/10688465/programmatically-resizing-edittext-on-orientation-change