0

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

具有相等填充的线性布局中的子级

因此,我想我会交换宽度/高度属性,以便所有元素的宽度都是 wrap_content,而高度则是 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>

我觉得我很接近 - 我在这里错过了什么?

4

2 回答 2

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>
于 2014-07-11T12:35:36.050 回答
0

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

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

所以按钮只会和给定的背景图像一样宽。

如果您的背景图像太大,那也可能会歪斜。

于 2014-06-29T20:34:35.523 回答