【问题标题】:Linear Layout - fixed height children are wrongly placed线性布局 - 固定高度的孩子被错误地放置
【发布时间】:2017-05-29 08:28:27
【问题描述】:

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

这就是我得到的:

【问题讨论】:

    标签: android android-linearlayout android-xml


    【解决方案1】:

    只需在所有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>
    

    【讨论】:

    • 这有帮助,但不能解释为什么没有将体重均匀分布的固定身高儿童画在一条线上。有任何想法吗?我确定我已经像上面那样使用了大约 50 次,而且它总是有效...
    • 我还没有想过要尝试这个,我帮忙,谢谢
    • 欢迎,使用这个的原因可能是内容。随着内容的增加,布局开始显得杂乱无章。
    【解决方案2】:

    试试这个

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

    【讨论】:

    • 我不能使用android:weightSum="3",因为我可能会隐藏一些视图,只有一两个项目可见..
    【解决方案3】:

    试试这个

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

    【讨论】:

      【解决方案4】:

      将此行放在每个文本视图中android:singleLine="true" 这会将您的文本换成一行

      谁能解释问题出在哪里?没找到……

      问题是您的 Fixed Hight 64dp 实际您告诉查看该视图应在中心聚焦文本中扩展到 64 dp
      但是当文本比你的 textview 文本大时,文本将从所有侧面剪切 这个有三个选项

      1.make textview hight "WrapContent" 但在这种情况下,所有 textview 的高度与文本长度不同

      2.Make text single line android:singleLine="true" like Muted-Dun...这样基本上它在单行中显示您的文本

      3。设置 android:layout_gravity="center" 但在这种情况下,如果您的文本长度大于您的文本被剪切

      【讨论】:

        【解决方案5】:

        将属性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>
        

        输出:

        【讨论】:

          猜你喜欢
          • 2018-04-28
          • 1970-01-01
          • 1970-01-01
          • 2020-10-19
          • 1970-01-01
          • 2023-03-22
          • 2011-11-04
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多