【问题标题】:compound control sizing button to fit inside edittext复合控件大小调整按钮以适应edittext
【发布时间】:2011-04-06 16:24:08
【问题描述】:

在下面的 sn-p 中,我怎样才能让 mClickable 与 mTextArea 的高度相同?

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

【问题讨论】:

    标签: android view android-layout android-edittext imagebutton


    【解决方案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>
    

    【讨论】:

    • 这个问题措辞很尴尬,希望编辑能消除混乱。我只希望按钮与编辑文本的高度相同。
    • 我接受了答案,但如果您看到这个,我不确定如何使用权重来匹配尺寸?
    【解决方案2】:

    您是否有理由不在您的 xml 中专门设置它们的高度,以便确保它们具有相同的大小?喜欢你的按钮和你的edittext会有这个属性吗?

    android:layout_height="20dip"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-27
      • 1970-01-01
      • 2020-04-25
      • 2016-02-28
      • 1970-01-01
      • 2016-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多