【问题标题】:Android Layout - layoutweight and weightsumAndroid 布局 - layoutweight 和 weightsum
【发布时间】:2013-03-29 03:34:37
【问题描述】:

我需要使用一组线性布局构建一个布局。布局必须占据屏幕的定义百分比。我需要这样做才能在所有设备中看到相似的外观。

问题:

我在右上角布局中有一个文本视图(绿色框)。每当我在 textview 中添加一些数据时,它都会扰乱整个布局,如下图 2 所示。但是当数据到达屏幕/布局的右端时,我需要自动换行。

请帮我解决这个问题。提前致谢

Image1 - 根据下面显示的 XML 的图形视图

Image2 - 当我在 textView 中添加一些数据时,显示布局如何自动更改

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical" 
android:weightSum="100">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_weight="91"
    android:weightSum="100">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:orientation="vertical" 
                        android:layout_weight="40"
                        android:weightSum="235">

                                                    <LinearLayout
                                                        android:layout_width="match_parent"
                                                        android:layout_height="match_parent" 
                                                        android:orientation="vertical"  
                                                        android:gravity="center"
                                                        android:background="#ff0000"
                                                        android:layout_weight="100">

                                                                                    <TextView
                                                                                        android:id="@+id/textView1"
                                                                                        android:layout_width="wrap_content"
                                                                                        android:layout_height="wrap_content"
                                                                                        android:textColor="#000000" />

                                                    </LinearLayout>

                                                    <LinearLayout
                                                        android:layout_width="match_parent"
                                                        android:layout_height="match_parent" 
                                                        android:orientation="vertical"   
                                                        android:background="#ffff00"
                                                        android:layout_weight="45">   



                                                                                    <ViewFlipper
                                                                                        android:id="@+id/view_flipper"
                                                                                        android:layout_width="match_parent"
                                                                                        android:layout_height="match_parent"
                                                                                        android:layout_below="@+id/tvItemName" >


                                                                                                                        <LinearLayout
                                                                                                                            android:layout_width="match_parent"
                                                                                                                            android:layout_height="match_parent"
                                                                                                                            android:orientation="vertical" >
                                                                                        </LinearLayout>
                                                                                     </ViewFlipper>



                                                    </LinearLayout>

                                                    <LinearLayout
                                                        android:layout_width="match_parent"
                                                        android:layout_height="match_parent" 
                                                        android:orientation="vertical"  
                                                        android:background="#ffffff"
                                                        android:layout_weight="90">   


                                                    </LinearLayout> 

                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent"
                        android:orientation="vertical" 
                        android:layout_weight="60"
                        android:weightSum="100">

                                                    <LinearLayout
                                                        android:layout_width="match_parent"
                                                        android:layout_height="match_parent" 
                                                        android:orientation="vertical"   
                                                        android:layout_weight="30"
                                                        android:background="#00ab00"
                                                        android:weightSum="100"> 

                                                                                    <TextView
                                                                                        android:id="@+id/textView1"
                                                                                        android:layout_width="match_parent"
                                                                                        android:layout_height="fill_parent"
                                                                                        android:textColor="#000000"
                                                                                        android:text="Test data" />

                                                    </LinearLayout>

                                                    <LinearLayout
                                                        android:layout_width="match_parent"
                                                        android:layout_height="match_parent" 
                                                        android:orientation="vertical" 
                                                        android:background="#cd00ab" 
                                                        android:layout_weight="70"> 

                                                    </LinearLayout>

                    </LinearLayout>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:background="#ab0000"
    android:layout_weight="9">

</LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: android android-layout textview android-listfragment


    【解决方案1】:

    layout_widthlayout_height 开头为 0dp 而不是 wrap_content。 (如果是垂直布局,layout_height应该是0dp;如果是水平布局,layout_width应该是0dp)

    layout_weight 描述了在分配了layout_widths/layout_heights 之后剩余空间如何划分。

    请注意,wrap_content 并不意味着“使文本换行”;这意味着将该维度设置为视图的“首选”维度。

    【讨论】:

    • 我仍然无法解决这个问题。如果您能指导我更正xml,我会很有帮助。非常感谢您的帮助
    • 我同意If it's in a vertical layout, layout_height should be 0dp; if it's in a horizontal layout, layout_width should be 0dp。你找到反例了吗?
    • @Suzi - 一个反例是您希望确保组件始终具有某个最小空间量的情况。如果您有两个组件,其中看到一个总是比看到另一个更重要,这可能会有所帮助。但是,这意味着您正在考虑组件位于可能不可见的区域中,并且您应该真正考虑不同的 UI 方法,可能是 ScrollView、选项卡或单独的活动/片段。
    【解决方案2】:

    使用此 XML 文件。我已经为您进行了更改。


    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    android:orientation="vertical"
    android:weightSum="100" >
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="91"
        android:orientation="horizontal"
        android:weightSum="100" >
    
        <!-- Below is the first modification to layout_width -->
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="40"
            android:orientation="vertical"
            android:weightSum="235" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="100"
                android:background="#ff0000"
                android:gravity="center"
                android:orientation="vertical" >
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="#000000" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="45"
                android:background="#ffff00"
                android:orientation="vertical" >
    
                <ViewFlipper
                    android:id="@+id/view_flipper"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_below="@+id/tvItemName" >
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical" >
                    </LinearLayout>
                </ViewFlipper>
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="90"
                android:background="#ffffff"
                android:orientation="vertical" >
            </LinearLayout>
        </LinearLayout>
    
        <!-- Below is the second modification to layout_width -->
    
        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="60"
            android:orientation="vertical"
            android:weightSum="100" >
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="30"
                android:background="#00ab00"
                android:orientation="vertical"
                android:weightSum="100" >
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"
                    android:text="Test data Test dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest data"
                    android:textColor="#000000" />
            </LinearLayout>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="70"
                android:background="#cd00ab"
                android:orientation="vertical" >
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="9"
        android:background="#ab0000" >
    </LinearLayout>
    
    </LinearLayout>
    

    我觉得很好。看看下面的快照。

    【讨论】:

    • 检查我编辑的答案。试试看,让我知道。粘贴时缺少线性布局的开始和结束标记。
    • 谢谢..现在很好..文本没有按不同大小放大。请有任何指示。我不想要不同设备的单独布局picpaste.com/4-CY0Qb7o0.png
    • android:textSize ="XXsp" 这里 XX 是一些数值。一个布局不能适合所有设备。由你决定。如果您发现我的回答有用,请将其标记为接受。快乐编码。
    猜你喜欢
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 2012-10-20
    • 1970-01-01
    相关资源
    最近更新 更多