简体   繁体   中英

Trying to space buttons evenly for any screen in a LinearLayout, without stretching the images

I gave the answer in this question a try, the only difference being that I'm using a vertical LinearLayout instead of horizontal:

Children in Linear Layout with equal padding

As such, I figured that I would swap around the width/height attributes so that width for all elements would be wrap_content, where as height would be match_parent. Is that how it should be?

Anyway, my weight selection is still causing my buttons to stretch in horrible ways.

Here is the xml file:

   <LinearLayout
        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:gravity="center_horizontal"
        android:orientation="vertical"
        tools:context=".MainActivity" >

        <ImageView
            android:id="@+id/logo"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:scaleX="0.8"
            android:scaleY="0.8"
            android:src="@drawable/logo" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.4"
            android:background="@drawable/button1_image"
            android:onClick="button1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.4"
            android:background="@drawable/button2_image"
            android:onClick="button2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.4"
            android:background="@drawable/button3_image"
            android:onClick="button3" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.4"
            android:background="@drawable/button4_image"
            android:onClick="button4" />

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.4"
            android:onClick="Button5"
            android:text="Text"
            android:textSize="26dp" />
    </LinearLayout>

I feel like I'm close - what am I missing here?

If you want to prevent an item from being stretched, set it's height and width attributes to

android:layout_width="wrap_content"
android:layout_height="wrap_content"

So the buttons will only be as wide as the background image given.

If your background image is too large, that can be skewing things as well.

i don't know excetly what you require, but is this helpful to you?

<LinearLayout 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:gravity="center_horizontal"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:scaleX="0.8"
        android:scaleY="0.8"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/ic_launcher"
        android:onClick="button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/ic_launcher"
        android:onClick="button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/ic_launcher"
        android:onClick="button3" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/ic_launcher"
        android:onClick="button4" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:onClick="Button5"
        android:text="Text"
        android:textSize="26dp" />

</LinearLayout>

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