Android EditText以编程方式调整大小

时间:2012-07-15 14:30:53

标签: android resize android-edittext

我有一个视图,其中我有两个LinearLayouts - 一个带有Text,EditText和一个Button,另一个带有Text和EditText。我试图获得第二个EditText的宽度(在第二行)以匹配第一行的宽度。我已经尝试将其设置为TableLayout而不是仅使用LinearLayouts,现在我尝试以编程方式设置宽度。我得到第一个EditText的宽度并尝试第二个EditText的setWidth,但它不会改变大小。

* JAVA RESIZE ATTEMPT *

@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 LAYOUT *

<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>

*来自EMULATOR的屏幕* Screenshot from Emulator

3 个答案:

答案 0 :(得分:1)

我用一种对我来说最有意义的不同方式解决了它。我所做的是通过getWidth获取铅笔按钮的宽度,然后将LinearLayout的右边填充(每行是它自己的LinearLayout)设置为该按钮的宽度。这似乎完美地工作,并允许按钮改变大小(基于屏幕大小和DP)和填充相应的反应。

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

widthOfEditButton = imageButtonWidth.getWidth();

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

答案 1 :(得分:0)

你可能想要这样做

getWidthBox.getLayoutParams().width=getWidthBox.getWidth();
 setWidthBox.setWidth(getWidthBox.getWidth());

答案 2 :(得分:0)

我只使用相对布局并将第二个TextView对齐第一个

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/TV01"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_marginTop="8dp"
        android:text="Test Text1: "
        android:textSize="18dp" />

    <EditText
        android:id="@+id/ET01"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_toLeftOf="@+id/Btn"
        android:layout_toRightOf="@+id/TV01"
        android:text="test text" />

    <Button
        android:id="@+id/Btn"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:text="myBtn" />

    <TextView
        android:id="@+id/TV02"
        android:layout_width="wrap_content"
        android:layout_height="40dp"
        android:layout_below="@+id/TV01"
        android:layout_marginTop="8dp"
        android:text="Text2: "
        android:textSize="18dp" />

    <EditText
        android:id="@+id/ET02"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_alignLeft="@+id/ET01"
        android:layout_alignRight="@+id/ET01"
        android:layout_below="@+id/TV01"
        android:text="test text" />

</RelativeLayout>