2011-04-06 39 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" 
相关问题