Android的调整大小的EditText编程方式 [英] Android EditText Resize Programatically

查看:142
本文介绍了Android的调整大小的EditText编程方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我有两个LinearLayouts视图 - 一个文字,和的EditText一个按钮和一个只用文本和EditText上。我想获得第二的EditText的宽度(第二行)来匹配第一线的宽度。我试图使它成为一个TableLayout,而不是仅仅使用LinearLayouts现在正尝试以编程方式设置宽度。我得到的第一的EditText的宽度,并尝试在第二的EditText的setWidth,但它不改变大小。

I have a view in which I have two LinearLayouts - one with Text, EditText and a Button and one with just Text and EditText. I am trying to get the width of the second EditText (on the second line) to match the width of the first line. I have tried making it a TableLayout instead of just using LinearLayouts and now am trying to set the width programatically. I get the width of the first EditText and try to setWidth of the second EditText, but it doesn't change sizes.

* JAVA调整大小尝试*

@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);

 getWidthBox = (EditText) findViewById(R.id.editText10);
 setWidthBox = (EditText) findViewById(R.id.EditText01);

 Toast.makeText(this, "Meaured Width: " + Integer.toString(getWidthBox.getMeasuredWidth()), Toast.LENGTH_SHORT).show();
 Toast.makeText(this, "Get Width: " + Integer.toString(getWidthBox.getWidth()), Toast.LENGTH_SHORT).show();

 //Neither of these setWidth Lines Work Correctly
 setWidthBox.getLayoutParams().width=getWidthBox.getWidth();
 setWidthBox.setWidth(getWidthBox.getWidth());
}

* XML布局*

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.15"
        android:paddingLeft="5dp" >


        <TextView
            android:id="@+id/textView2"
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:text="Points Allowed: "
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="15sp" />

        <EditText
            android:id="@+id/editText10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number" >
        </EditText>

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:src="@drawable/editpencil" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.15"
        android:paddingLeft="5dp" >

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="110dp"
            android:layout_height="wrap_content"
            android:text="Points Used: "
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textSize="15sp" />

        <EditText
            android:id="@+id/EditText01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number" />
    </LinearLayout>

* FROM SCREENSHOT仿真器*

* SCREENSHOT FROM EMULATOR *

推荐答案

我解决它在一个有点不同的方式,使最有意义的我。我所做的就是通过获得的getWidth铅笔按钮的宽度,然后设置的LinearLayout的右填充(每行是其自身的LinearLayout)到该按钮的宽度。这似乎很好地工作,并允许按钮来改变大小(根据屏幕大小和DP)和填充,以做出相应的反应。

I solved it in a bit of a different way that makes the most sense to me. What I did was get the width of the pencil button via getWidth and then set the right-padding of the LinearLayout (each row is its own LinearLayout) to the width of that button. This seems to work perfectly and allows for the button to change size (based on screen size and DP) and the padding to react accordingly.

imageButtonWidth = (ImageButton) findViewById(R.id.imageButtonEdit);
linearLayoutPointsUsed= (LinearLayout) findViewById(R.id.linearPointsUsed);

widthOfEditButton = imageButtonWidth.getWidth();

linearLayoutPointsUsed.setPadding(5, 0, widthOfEditButton, 0);

这篇关于Android的调整大小的EditText编程方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆