【问题标题】:Image and Button as same height using LinearLayout?使用LinearLayout的图像和按钮高度相同?
【发布时间】:2018-11-16 23:36:04
【问题描述】:

我正在使用带有 weightSum 的 LinearLayout,因此我可以设计通用屏幕以在所有屏幕尺寸上运行。也许我无法正确理解 wrap_content 和 match_parent 属性。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
          tools:context="com.hbss.hybridsoftwaresolutions.rashidfaheem.mysecondapplication.easypercentagecalculator.MainActivity"
    tools:showIn="@layout/activity_main"
    android:orientation="vertical"
    android:weightSum="10"
    android:background="#0d001c"
    >

    <View
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1">

    </View>

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:weightSum="5"
    >
<ImageView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:src="@drawable/image01"

    />
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Percent Calculator"
        android:textSize="15dp"
        android:layout_weight="3"
        android:id="@+id/btnPCalculator"
        />
    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1">

    </View>


</LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="5"
        >
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/image04"

            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Percent Increase"
            android:textSize="15dp"
            android:layout_weight="3"
            android:id="@+id/btnPIncrease"
            />
        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

        </View>


</LinearLayout>







    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="5"
        >
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/image05"

            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Percent Decrease"
            android:textSize="15dp"
            android:layout_weight="3"
            android:id="@+id/btnPDecrease"
            />
        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

        </View>
</LinearLayout>








    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="5"
        >
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/image02"

            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Tip Calculator"
            android:textSize="15dp"
            android:layout_weight="3"
            android:id="@+id/btnTCalculator"
            />
        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

        </View>
</LinearLayout>






        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:weightSum="5"
            >
            <ImageView
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/image03"

                />
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="Percent Margin"
                android:textSize="15dp"
                android:layout_weight="3"
                android:id="@+id/btnPMargin"
                />
            <View
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1">

            </View>

    </LinearLayout>





    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="5"
        >
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/image06"

            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Percentage Change"
            android:textSize="15dp"
            android:layout_weight="3"
            android:id="@+id/btnPChange"
            />
        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

        </View>
        </LinearLayout>




    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="5"
        >
        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/image07"

            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="Percentage (what % of)"
            android:textSize="15dp"
            android:layout_weight="3"
            android:id="@+id/btnPWhatof"
            />
        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

        </View>
    </LinearLayout>

    </LinearLayout>

【问题讨论】:

    标签: image button height


    【解决方案1】:

    问题可能是您的 ImageView 和 Button 高度。尝试将它们设置为 match_parent 而不是 wrap_content:

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="5">
    
        <ImageView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@drawable/image01"/>
    
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:text="Percent Calculator"
            android:textSize="15dp"
            android:layout_weight="3"
            android:id="@+id/btnPCalculator"/>
        <View
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    

    您应该使用 match_parent,这样 TextView 和 Button 的高度都会填满您的 LinearLayout 中的所有可用空间。如果您使用 wrap_content,它们只会占用他们需要的空间,而忽略剩余的可用空间。

    【讨论】:

    • 当我将 match_parent 用于图像和按钮时,它们会占用整个屏幕空间。实际上我正在使用两个线性布局。第一个具有垂直方向的垂直方向填充 7 行块(图像+按钮),并且在该垂直布局中,每行块(图像+按钮)都有自己的水平线性布局,可以调整图像+按钮+空白空间(视图)重量。
    • 好的。我编辑了我的帖子并粘贴了整个 xml 代码。我应该怎么做才能使图像和按钮具有相同的高度?
    猜你喜欢
    • 2013-11-20
    • 2016-07-27
    • 2014-05-18
    • 1970-01-01
    • 2017-02-25
    • 2017-04-13
    • 1970-01-01
    • 2018-01-30
    • 1970-01-01
    相关资源
    最近更新 更多