2

2 つの LinearLayouts があるビューがあります。1 つは Text、EditText、および Button で、もう 1 つは Text と EditText だけです。最初の行の幅と一致するように、2 番目の EditText (2 行目) の幅を取得しようとしています。LinearLayouts を使用する代わりに TableLayout を作成しようとしましたが、プログラムで幅を設定しようとしています。最初の EditText の幅を取得し、2 番目の EditText の幅を設定しようとしましたが、サイズは変わりません。

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

*エミュレーターからのスクリーンショット* エミュレーターのスクリーンショット

4

3 に答える 3

2

私はそれを少し別の方法で解決しました。これは私にとって最も理にかなっています。私がしたことは、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);
于 2012-07-17T14:27:40.993 に答える
0

相対レイアウトを使用し、2番目の TextView を最初の TextView に alignRight します

これを試して:

<?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>
于 2012-07-15T15:02:06.620 に答える
0

あなたはおそらくこれをしたい

getWidthBox.getLayoutParams().width=getWidthBox.getWidth();
 setWidthBox.setWidth(getWidthBox.getWidth());
于 2012-07-15T14:33:55.570 に答える