Android的布局 - layoutweight和weightsum [英] Android Layout - layoutweight and weightsum

查看:129
本文介绍了Android的布局 - layoutweight和weightsum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要建立与组线性布局的布局。布局具有占据一个限定的屏幕的比例。我需要做这在所有的设备相似的外观。

问题:

我在右上角的布局(绿色方框)一个TextView。每当我在TextView中添加一些数据,它扰乱作为第二图象显示在下面的整个布局。但我需要的时候它到达屏幕/布局的右端有自动数据套。

请帮我解决这个问题。在此先感谢

Image1的 - 图形视图按如下

的XML

IMAGE2 - 显示了如何布局自动改变,当我在TextView中

添加一些数据

XML

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:背景=@可绘制/背景
机器人:方向=垂直
机器人:weightSum =100>

<的LinearLayout
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=横向
    机器人:layout_weight =91
    机器人:weightSum =100>

                    <的LinearLayout
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =match_parent
                        机器人:方向=垂直
                        机器人:layout_weight =40
                        机器人:weightSum =235>

                                                    <的LinearLayout
                                                        机器人:layout_width =match_parent
                                                        机器人:layout_height =match_parent
                                                        机器人:方向=垂直
                                                        机器人:重力=中心
                                                        机器人:后台=#FF0000
                                                        机器人:layout_weight =100>

                                                                                    <的TextView
                                                                                        机器人:ID =@ + ID / textView1
                                                                                        机器人:layout_width =WRAP_CONTENT
                                                                                        机器人:layout_height =WRAP_CONTENT
                                                                                        机器人:文字颜色=#000000/>

                                                    < / LinearLayout中>

                                                    <的LinearLayout
                                                        机器人:layout_width =match_parent
                                                        机器人:layout_height =match_parent
                                                        机器人:方向=垂直
                                                        机器人:后台=#FFFF00
                                                        机器人:layout_weight =45>



                                                                                    < ViewFlipper
                                                                                        机器人:ID =@ + ID / view_flipper
                                                                                        机器人:layout_width =match_parent
                                                                                        机器人:layout_height =match_parent
                                                                                        机器人:layout_below =@ + ID / tvItemName>


                                                                                                                        <的LinearLayout
                                                                                                                            机器人:layout_width =match_parent
                                                                                                                            机器人:layout_height =match_parent
                                                                                                                            机器人:方向=垂直>
                                                                                        < / LinearLayout中>
                                                                                     < / ViewFlipper>



                                                    < / LinearLayout中>

                                                    <的LinearLayout
                                                        机器人:layout_width =match_parent
                                                        机器人:layout_height =match_parent
                                                        机器人:方向=垂直
                                                        机器人:后台=#FFFFFF
                                                        机器人:layout_weight =90>


                                                    < / LinearLayout中>

                    < / LinearLayout中>

                    <的LinearLayout
                        机器人:layout_width =WRAP_CONTENT
                        机器人:layout_height =match_parent
                        机器人:方向=垂直
                        机器人:layout_weight =60
                        机器人:weightSum =100>

                                                    <的LinearLayout
                                                        机器人:layout_width =match_parent
                                                        机器人:layout_height =match_parent
                                                        机器人:方向=垂直
                                                        机器人:layout_weight =30
                                                        机器人:后台=#00ab00
                                                        机器人:weightSum =100>

                                                                                    <的TextView
                                                                                        机器人:ID =@ + ID / textView1
                                                                                        机器人:layout_width =match_parent
                                                                                        机器人:layout_height =FILL_PARENT
                                                                                        机器人:文字颜色=#000000
                                                                                        机器人:文本=测试数据/>

                                                    < / LinearLayout中>

                                                    <的LinearLayout
                                                        机器人:layout_width =match_parent
                                                        机器人:layout_height =match_parent
                                                        机器人:方向=垂直
                                                        机器人:后台=#cd00ab
                                                        机器人:layout_weight =70>

                                                    < / LinearLayout中>

                    < / LinearLayout中>

< / LinearLayout中>

<的LinearLayout
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:后台=#ab0000
    机器人:layout_weight =9>

< / LinearLayout中>

< / LinearLayout中>
 

解决方案

使用此XML文件。我所做的更改给你。


 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:背景=@可绘制/背景
机器人:方向=垂直
机器人:weightSum =100>

<的LinearLayout
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_weight =91
    机器人:方向=横向
    机器人:weightSum =100>

    &所述;! - 下面是第一修改layout_width  - >

    <的LinearLayout
        机器人:layout_width =0dp
        机器人:layout_height =match_parent
        机器人:layout_weight =40
        机器人:方向=垂直
        机器人:weightSum =235>

        <的LinearLayout
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:layout_weight =100
            机器人:后台=#FF0000
            机器人:重力=中心
            机器人:方向=垂直>

            <的TextView
                机器人:ID =@ + ID / textView1
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:文字颜色=#000000/>
        < / LinearLayout中>

        <的LinearLayout
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:layout_weight =45
            机器人:后台=#FFFF00
            机器人:方向=垂直>

            < ViewFlipper
                机器人:ID =@ + ID / view_flipper
                机器人:layout_width =match_parent
                机器人:layout_height =match_parent
                机器人:layout_below =@ + ID / tvItemName>

                <的LinearLayout
                    机器人:layout_width =match_parent
                    机器人:layout_height =match_parent
                    机器人:方向=垂直>
                < / LinearLayout中>
            < / ViewFlipper>
        < / LinearLayout中>

        <的LinearLayout
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:layout_weight =90
            机器人:后台=#FFFFFF
            机器人:方向=垂直>
        < / LinearLayout中>
    < / LinearLayout中>

    &所述;! - 下面是第二修改layout_width  - >

    <的LinearLayout
        机器人:layout_width =0dp
        机器人:layout_height =match_parent
        机器人:layout_weight =60
        机器人:方向=垂直
        机器人:weightSum =100>

        <的LinearLayout
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:layout_weight =30
            机器人:后台=#00ab00
            机器人:方向=垂直
            机器人:weightSum =100>

            <的TextView
                机器人:ID =@ + ID / textView1
                机器人:layout_width =match_parent
                机器人:layout_height =FILL_PARENT
                机器人:文本=测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据测试数据
                机器人:文字颜色=#000000/>
        < / LinearLayout中>

        <的LinearLayout
            机器人:layout_width =match_parent
            机器人:layout_height =match_parent
            机器人:layout_weight =70
            机器人:后台=#cd00ab
            机器人:方向=垂直>
        < / LinearLayout中>
    < / LinearLayout中>
< / LinearLayout中>

<的LinearLayout
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_weight =9
    机器人:后台=#ab0000>
< / LinearLayout中>

< / LinearLayout中>
 

这看起来好像没什么问题。看看下面的快照。

  

  

I need to build a layout with the set of linear layouts. The layout has to occupy a defined percentage of the screen. I need to do this to have a similar look in all the devices.

Issue:

I have a textview in the top right layout (green color box ). Whenever I add some data in the textview it disturbs the entire layout as shown below in the 2nd image. But I need to have the data wraps automatically when it reaches the right end of the screen/layout.

Please help me to resolve this. Thanks In Advance

Image1 - Graphical view as per the XML shown below

Image2 - Shows how the layout automatically changes, when I add some data in the 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>

解决方案

Use this XML file. I have made the changes for you.


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

It looks fine to me. check out the below snapshot.

这篇关于Android的布局 - layoutweight和weightsum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆