Android LinearLayout高度与元素的宽度不成比例

时间:2017-07-01 08:57:01

标签: android android-layout height android-linearlayout

我想制作布局,将5个按钮均匀地放在任何屏幕尺寸的行中。为实现这一点,我做了以下布局:

<FrameLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/main_background_v3"
    tools:context="com.magnifi.pennantrace.training.TrainingSelectFragment">
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="5"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent">
            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@mipmap/training_stretch"
                android:layout_weight="1"/>
            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@mipmap/training_mechanics"
                android:layout_weight="1"/>
            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@mipmap/training_cardio"
                android:layout_weight="1"/>
            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@mipmap/training_weights"
                android:layout_weight="1"/>
            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@mipmap/training_batfield"
                android:layout_weight="1"/>
        </LinearLayout>
    </android.support.constraint.ConstraintLayout>
</FrameLayout>

它做了我想要的,但现在身高不正确。所有按钮图像均为正方形。这就是我所拥有的:

如何设置合适的高度(与按钮宽度成比例拟合)? enter image description here

3 个答案:

答案 0 :(得分:1)

<FrameLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/main_background_v3"
    tools:context="com.magnifi.pennantrace.training.TrainingSelectFragment">
    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="5"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintLeft_toLeftOf="parent">
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@mipmap/training_stretch"
                android:background="@android:color/transparent"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                android:layout_weight="1"/>
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@mipmap/training_mechanics"
                android:background="@android:color/transparent"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                android:layout_weight="1"/>
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@mipmap/training_cardio"
                android:background="@android:color/transparent"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                android:layout_weight="1"/>
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@mipmap/training_weights"
                android:background="@android:color/transparent"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                android:layout_weight="1"/>
            <ImageButton
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:src="@mipmap/training_batfield"
                android:background="@android:color/transparent"
                android:scaleType="fitCenter"
                android:adjustViewBounds="true"
                android:layout_weight="1"/>
        </LinearLayout>
    </android.support.constraint.ConstraintLayout>

    </FrameLayout>

答案 1 :(得分:0)

使用您的代码替换波纹管编写的代码将解决您的问题。

<FrameLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/main_background_v3"
tools:context="com.magnifi.pennantrace.training.TrainingSelectFragment">
<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="5"
        android:layout_marginTop="16dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent">
        <ImageButton
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@mipmap/training_stretch"
            android:layout_weight="1"/>
        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@mipmap/training_mechanics"
            android:layout_weight="1"/>
        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@mipmap/training_cardio"
            android:layout_weight="1"/>
        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@mipmap/training_weights"
            android:layout_weight="1"/>
        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@mipmap/training_batfield"
            android:layout_weight="1"/>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

答案 2 :(得分:0)

如果您使用线性布局和权重请勿忘记填写所有neccissary文件夹:xxxhdpi,xxhdpi,xhdpi,hdpi,mdpi!