Android的调整大小的EditText编程(Android EditText Resize Pr

2019-10-17 00:19发布

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

* 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仿真器*

Answer 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);


Answer 2:

你可能想这样做

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


Answer 3:

我只想用相对布局和alignRight第二的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>


文章来源: Android EditText Resize Programmatically