尝试均匀地为LinearLayout中的任何屏幕分隔按钮,而不拉伸图像

时间:2014-06-29 20:28:10

标签: android xml layout android-linearlayout android-layout-weight

我试试这个问题的答案,唯一的区别是我使用垂直的LinearLayout而不是水平:

Children in Linear Layout with equal padding

因此,我想我会交换宽度/高度属性,以便所有元素的宽度都是wrap_content,其中height将是match_parent。这应该是怎么回事?

无论如何,我的体重选择仍然导致我的按钮以可怕的方式伸展。

这是xml文件:

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

我觉得我很接近 - 我在这里失踪了什么?

2 个答案:

答案 0 :(得分:0)

如果要防止项目被拉伸,请将其高度和宽度属性设置为

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

因此按钮只会与给出的背景图像一样宽。

如果您的背景图片太大,那也可能会使事情发生偏差。

答案 1 :(得分:0)

我不知道你需要什么,但这对你有帮助吗?

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