2012-07-15 27 views
1

我有一個視圖,其中有兩個LinearLayouts - 一個帶有Text,EditText和一個按鈕,另一個帶有Text和EditText。我想獲得第二個EditText的寬度(在第二行)以匹配第一行的寬度。我已經嘗試使它成爲一個TableLayout而不是僅僅使用LinearLayouts,現在我試圖以編程方式設置寬度。我得到第一個EditText的寬度,並嘗試第二個EditText的setWidth,但它不會更改大小。Android 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> 

* FROM SCREENSHOT仿真器* Screenshot from Emulator

回答

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

你probabl Ÿ要做到這一點

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

第二個EditText仍然會進入屏幕的邊緣。 我附上了第一篇文章的截圖。 – mattdonders 2012-07-15 14:50:18

0

我只想用相對佈局和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> 
+0

你能提供一些代碼來幫助我嗎?我嘗試將整個LinearLayout放入一個RelativeLayout中,然後將alignRight代碼添加到下面 - 不知道爲什麼我的代碼不會出現在下面。 ''' – mattdonders 2012-07-15 15:14:07

相關問題