线性布局 - 错误放置固定高度的儿童

时间:2017-05-29 08:28:28

标签: android android-linearlayout android-xml

我将3 TextView放在LinearLayout中,并希望它们等分配并具有固定的高度。 layout_weight和固定layout_height的组合应该确保所有3个视图始终显示在一行中并始终具有相同的高度。但他们不是。

有人可以解释问题所在吗?我无法找到它......

这是我的LinearLayout和它的孩子:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tvMuted"
        android:text="@string/color_palette_muted"
        android:foreground="?selectableItemBackground"
        android:gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_margin="4dp"
        android:layout_height="64dp">
    </TextView>

    <TextView
        android:id="@+id/tvMutedLight"
        android:text="@string/color_palette_muted_light"
        android:foreground="?selectableItemBackground"
        android:gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_margin="4dp"
        android:layout_height="64dp">
    </TextView>

    <TextView
        android:id="@+id/tvMutedDark"
        android:text="@string/color_palette_muted_dark"
        android:foreground="?selectableItemBackground"
        android:gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_margin="4dp"
        android:layout_height="64dp">
    </TextView>

</LinearLayout>

这就是我得到的:

enter image description here

5 个答案:

答案 0 :(得分:2)

只需在所有TextView中设置android:layout_gravity="center",如下所示:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tvMuted"
        android:text="@string/color_palette_muted"
        android:foreground="?selectableItemBackground"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_margin="4dp"
        android:layout_height="64dp">
    </TextView>

    <TextView
        android:id="@+id/tvMutedLight"
        android:text="@string/color_palette_muted_light"
        android:foreground="?selectableItemBackground"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_margin="4dp"
        android:layout_height="64dp">
    </TextView>

    <TextView
        android:id="@+id/tvMutedDark"
        android:text="@string/color_palette_muted_dark"
        android:foreground="?selectableItemBackground"
        android:gravity="center"
        android:layout_gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_margin="4dp"
        android:layout_height="64dp">
    </TextView>

</LinearLayout>

答案 1 :(得分:0)

试试这个

    <LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:weightSum="3"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tvMuted"
        android:layout_width="0dp"
        android:layout_height="64dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:foreground="?selectableItemBackground"
        android:gravity="center"
        android:text="MUTED"/>

    <TextView
        android:id="@+id/tvMutedLight"
        android:layout_width="0dp"
        android:layout_height="64dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:foreground="?selectableItemBackground"
        android:gravity="center"
        android:text="MUTED-HELL"/>

    <TextView
        android:id="@+id/tvMutedDark"
        android:layout_width="0dp"
        android:layout_height="64dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:foreground="?selectableItemBackground"
        android:gravity="center"
        android:text="MUTED-DUNKEL"/>

</LinearLayout>

答案 2 :(得分:0)

试试这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ncrypted.demoapplications.Main2Activity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/tvMuted"
        android:layout_width="0dp"
        android:layout_height="64dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:background="#F00"
        android:gravity="center"
        android:text="color1" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="64dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:background="#F00"
        android:gravity="center"
        android:text="color1" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="64dp"
        android:layout_margin="4dp"
        android:layout_weight="1"
        android:background="#F00"
        android:gravity="center"
        android:text="color1" />


</LinearLayout>

答案 3 :(得分:0)

在每个文本视图中放置此行android:singleLine="true"这将以单行包装文本

  

有人可以解释问题所在吗?我找不到......

问题是你的固定高度64dp你被告知要查看该视图应该以中心为中心的文本扩展到64 dp 但是当文本大于文本视图时,文本将从所有方面剪切 这个

有三个选项

1 。制作textview hight“ WrapContent ”,但在这种情况下,所有textview的文字长度都不同

2 。制作文字单行 android:singleLine =“true”就像Muted-Dun ...这样基本上它以单行显示你的文字

第3 即可。设置 android:layout_gravity =“center”,但在这种情况下,如果您的文字长度大于文字被剪切

答案 4 :(得分:0)

将属性android:weightSum="3"android:gravity="center"添加到LinearLayout

以下是工作代码:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="3"
    android:gravity="center">

    <TextView
        android:id="@+id/tvMuted"
        android:text="Muted"
        android:foreground="?selectableItemBackground"
        android:gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_margin="4dp"
        android:layout_height="64dp"
        android:textColor="@android:color/white"
        android:background="#958F81">
    </TextView>

    <TextView
        android:id="@+id/tvMutedLight"
        android:text="Muted - Hell"
        android:foreground="?selectableItemBackground"
        android:gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_margin="4dp"
        android:layout_height="64dp"
        android:textColor="@android:color/white"
        android:background="#A4A38E">
    </TextView>

    <TextView
        android:id="@+id/tvMutedDark"
        android:text="Mmuted - Dunkel"
        android:foreground="?selectableItemBackground"
        android:gravity="center"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_margin="4dp"
        android:layout_height="64dp"
        android:textColor="@android:color/white"
        android:background="#686868">
    </TextView>

</LinearLayout>

<强>输出:

enter image description here

相关问题