在方向改变编程调整大小的EditText [英] Programmatically resizing EditText on orientation change
问题描述
我试图以编程方式调整的的EditText
时的电话改变方向,这是我到目前为止有:
私人最终浮动editTextWidthLandscap = 380;
私人最终浮动editTextWidthPortrait = 220;@覆盖
公共无效onConfigurationChanged(配置NEWCONFIG){
super.onConfigurationChanged(NEWCONFIG);
资源R = getResources();
浮动像素;
如果(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
像素= TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,editTextWidthLandscap,r.getDisplayMetrics());
input.setWidth((INT)像素); //输入是一个EditText
Toast.makeText(这一点,风景,Toast.LENGTH_SHORT).show();
}否则如果(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
像素= TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,editTextWidthPortrait,r.getDisplayMetrics());
input.setWidth((INT)像素); //输入是一个EditText
Toast.makeText(这一点,肖像,Toast.LENGTH_SHORT).show();
}
}
我也有这个在我的清单文件(如果它事项):
<活动
机器人:结果NAME =
机器人:configChanges =keyboardHidden |方向
机器人:主题=@安卓风格/ Theme.NoTitleBar
机器人:windowSoftInputMode =stateHidden>
< /活性GT;
但奇怪的是,它显示了吐司
得很好,但它似乎忽略 input.setWidth()
语句。如果任何人都可以解释为什么的EditText
不被调整大小和/或给我指针作为如何我将能够这样准确,将大大AP preciated。
编辑:
这是XML文件的一部分,在其中居住的EditText
<的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =80dp
机器人:背景=@色/紫
机器人:重力=CENTER_HORIZONTAL | center_vertical
机器人:方向=横向> <的EditText
机器人:ID =@ + ID /输入
机器人:layout_width =220DP
机器人:layout_height =50dp
机器人:背景=@绘制/ search_bar
机器人:提示=@字符串/ input_hint
安卓的inputType =TEXT | textNoSuggestions
机器人:MAXLINES =1
安卓了minHeight =50dp
安卓了minWidth =220DP
机器人:selectAllOnFocus =真
机器人:单线=真
机器人:文字颜色=@彩色/ black_grey
机器人:TEXTSIZE =16.67sp/> <按钮
机器人:ID =@ + ID / search_button
机器人:layout_width =65dp
机器人:layout_height =50dp
机器人:背景=@绘制/ search_button/>
< / LinearLayout中>
编辑2:(解决方案)
我还是不知道是什么 input.setWidth()
正是这么做,但我发现我不得不使用的LayoutParams
此问题。下面是code,现在是为我工作:
私人最终浮动editTextWidthLandscap = 380;
私人最终浮动editTextWidthPortrait = 220;
私人最终浮动editTextHeight = 50; @覆盖
公共无效onConfigurationChanged(配置NEWCONFIG){
super.onConfigurationChanged(NEWCONFIG);
资源R = getResources();
浮widthpx;
浮heightpx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,editTextHeight,r.getDisplayMetrics());;
如果(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE){
widthpx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,editTextWidthLandscap,r.getDisplayMetrics());
LinearLayout.LayoutParams LP =新LinearLayout.LayoutParams((INT)Math.round(widthpx),(INT)Math.round(heightpx));
trademarkTextfield.setLayoutParams(LP);
}否则如果(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
widthpx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,editTextWidthPortrait,r.getDisplayMetrics());
LinearLayout.LayoutParams LP =新LinearLayout.LayoutParams((INT)Math.round(widthpx),(INT)Math.round(heightpx));
trademarkTextfield.setLayoutParams(LP);
}
}
根据我自己的经验,有时候我们往往overthink的不良行为的原因。我发现不止一次的事情,我认为理所当然,是不是这样的。
您应该检查这两个案件的PX说法真的非常不同。我知道这是一个基础,但也许 applyDimension
不工作,你想到还是有其他地方定义一些最小宽度,你只要打它。
您也应该调用的getWidth()
前后调用 setWidth后()
。为什么?因为也许功能是否工作正常,系统确实觉得宽度haschanged,问题不在于此。
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);
}
}
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.
这篇关于在方向改变编程调整大小的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!