开发者

compound control sizing button to fit inside edittext

开发者 https://www.devze.com 2023-02-22 18:19 出处:网络
In the below snippet, how could I get mClickable to be the same height as mTextArea? public class EditSpinner extends LinearLayout {

In the below snippet, how could I get mClickable to be the same height as 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);
   开发者_Python百科     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));
    }
}


Its a bit difficult to tell exactly what you want, but it sounds like you are asking for two different things, which (generally) isn't possible with only one layout. Basically from what I interpret, you are saying I want the button to be the same size as the image, and then saying I don’t want the button to be the same size as the image (when its too big).

My suggestion is to incorporate a way to decipher which layout style you want. Perhaps it is by checking the image size vs the current screen size. Then apply the type of parameters that will work for that particular layout.

Setting the two the same size is easy via use of the XML android:layout_weight parameter, and you are already familiar with wrap_content for use with a larger image. While you can use these two parameters together you won't get what you want in all cases (again assuming I understand your post). You'll need one layout using weights to make the views the same size and another using wrap_content to allow for different sizes.

Also, just a side note, I would really recommend using XML for layouts unless there is something specific you can't accomplish with XML.

Edit: Here is an example for you.

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

compound control sizing button to fit inside edittext


is there a reason why you won't specifically set their height in your xml so that you are assured they are of the same size? like your button and your edittext will have this attribute?

android:layout_height="20dip"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号