简体   繁体   中英

Why does it look like my androidbutton is compressed in width?

I want to add multiple Buttons in my activity programmatically. That works fine so far, but it looks like my button is compressed in width. The button has a background image that is 20*20px and im using WRAP_CONTENT for width and height. Here is the code:

for(int i = 0; i < 5; i++){

        LinearLayout layout;
        Button buttonDel;

        layout = (LinearLayout)findViewById(R.id.linLayout);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.setOrientation(LinearLayout.VERTICAL);

        buttonDel = new Button(this);
        buttonDel.setId(i);
        buttonDel.setBackgroundResource(R.drawable.buttondelete);
        buttonDel.setLayoutParams(params);

        layout.addView(buttonDel);

    }

And the xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.developer.cardz.ListOfAllWordsActivity">

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/scrollView">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/linLayout">

    </LinearLayout>
</ScrollView>

Do you have any suggestions?

Let me do some suggestions.

Your code is more efficient this way:

    LinearLayout layout;
    Button buttonDel;

    layout = (LinearLayout)findViewById(R.id.linLayout);


    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    for(int i = 0; i < 5; i++){
        buttonDel = new Button(this);
        buttonDel.setId(i);
        buttonDel.setBackgroundResource(R.drawable.buttondelete);
        buttonDel.setLayoutParams(params);

        layout.addView(buttonDel);
    }

And layout.setOrientation(LinearLayout.VERTICAL); is not needed, since you have it on your XML.

Could you show the R.drawable.buttondelete? If it's a png9, it's normal it is shrunk. Try putting some text to the button.

I changed Button to ImageButton. That solved the problem. Thanks to all who have commented!

Just in case if you want to continue with Button , change

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

to

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(100, 100);

where (100, 100) is your desired button width and height .

Does it has any text inside them? If your button had any text on them, then those would be considered as "content" so it may compress image regarding to text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM