2011-04-06 43 views
0

在下面的代碼片段,我怎麼能拿mClickable是相同的高度mTextArea?複合控制按鈕大小以適應內部的EditText

public class EditSpinner extends LinearLayout { 

    EditText mTextArea; 
    ImageButton mClickable; 

    public EditSpinner(Context context, String[] options) { 
     super(context); 
     setOrientation(HORIZONTAL); 
     setGravity(Gravity.CENTER_VERTICAL); 
     setBackgroundResource(android.R.drawable.edit_text); 
     setAddStatesFromChildren(true); 
     setPadding(0, 0, 0, 0); 

     mTextArea = new EditText(context); 
     mTextArea.setSingleLine(true); 
     mTextArea.setBackgroundResource(0); 
     addView(mTextArea, new LayoutParams( 
       LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f)); 

     mClickable = new ImageButton(context); 
     mClickable.setBackgroundResource(0); 
     mClickable.setImageDrawable(
       context.getResources().getDrawable(
         android.R.drawable.btn_default_small)); 
     //Scale to mTextArea.getHeight()... except getHeight doesn't work 
     addView(mClickable, 
       new LayoutParams(WRAP_CONTENT, WRAP_CONTENT)); 
    } 
} 

回答

1

它有點難以分辨你想要什麼,但它聽起來像你所要求的兩個不同的東西,這(一般)是不可能的,只有一個佈局。基本上從我解釋,你說我想要的按鈕是大小的圖像一樣,然後說我希望按鈕是大小的圖像相同的(當其過大)。

我的建議是將一種方式來解讀你想要的佈局樣式。也許是通過檢查圖像大小與當前屏幕大小。然後應用適用於該特定佈局的參數類型。

通過使用XML android:layout_weight參數可以輕鬆設置兩個大小相同,並且您已經熟悉wrap_content以便與更大圖像一起使用。雖然你可以一起使用這兩個參數,但在任何情況下都不會得到你想要的(再次假設我理解你的帖子)。您需要一個使用權重的佈局來使視圖具有相同的大小,另一個佈局使用wrap_content來允許不同的大小。

而且,只是一個側面說明,我真的建議使用XML來佈局,除非有一些具體的事情你不能用XML完成。

編輯:下面是一個例子。

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

    <Button 
     android:id="@+id/button_1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="short" /> 

    <Button 
     android:id="@+id/button_2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="medium" /> 

    <Button 
     android:id="@+id/button_3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="loooooooooong" />  
</LinearLayout> 

enter image description here

+0

這個問題措手不及,希望編輯能夠消除混淆。我只希望按鈕的高度與edittext相同。 – 2011-04-06 17:26:19

+0

我接受了答案,但如果你看到這個,我不知道如何使用重量來匹配尺寸? – 2011-04-06 21:40:48

+0

發表了一個使用XML的例子。 – user432209 2011-04-07 00:54:10

0

是存在的,爲什麼你會不會專門設置自己的身高在XML中,讓你放心,他們都是一樣大小的原因是什麼?像你的按鈕,你的edittext會有這個屬性?

android:layout_height="20dip"