【问题标题】:Defining a percentage width for a LinearLayout? [duplicate]为 LinearLayout 定义百分比宽度? [复制]
【发布时间】:2011-07-02 13:15:22
【问题描述】:

我想为包含一些按钮的 LinearLayout 定义百分比宽度 (70%),以便我可以将其居中并让子按钮可以填充父级。这是一张显示我的意思的图片:

我目前的布局是这样的:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:id="@+id/layoutContainer" android:orientation="vertical">
    <LinearLayout android:layout_width="fill_parent"
        android:id="@+id/barContainer" android:orientation="horizontal"
        android:layout_height="40dp" android:background="@drawable/titlebackground">
        <ImageView android:id="@+id/barLogo" android:src="@drawable/titlelogo"
            android:layout_gravity="center_vertical" android:adjustViewBounds="true"
            android:layout_height="25dp" android:layout_width="wrap_content"
            android:scaleType="fitXY" android:paddingLeft="5dp"></ImageView>
    </LinearLayout>
    <TextView android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:gravity="center_horizontal"
        android:id="@+id/searchTip" android:text="@string/searchTip"
        android:paddingTop="10dp" android:paddingBottom="10dp"></TextView>
    <LinearLayout android:layout_height="wrap_content"
        android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="wrap_content">
        <Button android:text="Button" android:id="@+id/button1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:layout_width="wrap_content" android:id="@+id/button2" android:layout_height="wrap_content" android:text="Button"></Button>
        <Button android:layout_width="wrap_content" android:id="@+id/button3" android:layout_height="wrap_content" android:text="Button"></Button>
    </LinearLayout>
</LinearLayout>

im 所指的 LinearLayout 的 id 为:linearLayout1。我该怎么做?

【问题讨论】:

  • 感谢排名较高的问题所缺乏的出色图表。
  • 任何寻找演示的人都这样做goo.gl/s9mp2W

标签: android android-layout


【解决方案1】:

您必须设置元素的权重属性。创建三个 RelativeLayouts 作为您的 LinearLayout 的子项,并将权重设置为 0.15、0.70、0.15。然后将您的按钮添加到第二个 RelativeLayout(权重为 0.70 的那个)。

像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:id="@+id/layoutContainer" android:orientation="horizontal">
    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="0.15">
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="0.7">
        
        <!-- This is the part that's 70% of the total width. I'm inserting a LinearLayout and buttons.-->   
            <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:orientation="vertical">
                
                <Button 
                    android:text="Button1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                </Button>
                <Button
                    android:text="Button2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                </Button>
                <Button
                    android:text="Button3"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content">
                </Button>
                
            </LinearLayout>
        <!-- 70% Width End-->
        
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="0.15">
    </RelativeLayout>
</LinearLayout>

为什么权重是 0.15、0.7 和 0.15?因为总权重是 1,而 0.7 是总权重的 70%。

结果:

编辑:感谢@SimonVeloper 指出方向应该是水平的而不是垂直的,感谢@Andrew 指出权重可以是小数而不是整数。

【讨论】:

  • 这很hackish,但也很聪明,谢谢!
  • 在第一个按钮上,使用 android:layout_alignParentTop="true" 然后 android:layout_marginTop="50dp"。然后在第二个按钮上执行 android:layout_below="@+id/firstbutton"。以此类推。
  • 您可以使用小数设置权重:0.15、0.7、0.15
  • 在垂直方向的情况下重量不应该参考高度,在水平方向的情况下不应该参考宽度吗?
  • 可以将另外2个不可见的Relative Layout替换为View,并将layout_width/height设置为0dp,可见性设置为不可见。可能成本更低。
【解决方案2】:

我知道另一种解决方案,适用于重量:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent" 
              android:weightSum="10" 
              android:gravity="center_horizontal">

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" 
            android:layout_weight="7">
    </LinearLayout>
</LinearLayout>

【讨论】:

  • 我不知道为什么它的评分这么低,我想这比公认的解决方案要好得多!
  • @deathangel908 同意!而且干净多了!
  • 它很干净,但对我不起作用:内部 LinearLayout 占据了屏幕的整个宽度(至少在布局编辑器预览中)。您是说内部 LinearLayout 有android:layout_width="0dp" 吗?这就是文档所指出的 (developer.android.com/guide/topics/ui/layout/linear.html#Weight),它在布局编辑器预览中对我有用。
  • 这是一个更清洁的解决方案。但是,在嵌套布局而不是 layout_width="wrap_content" 中,它应该是 layout_width="0dp"
  • 简单好办法
【解决方案3】:

希望对你有帮助

<LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="horizontal">

    <LinearLayout android:layout_width="0dip"
        android:layout_height="wrap_content" android:orientation="horizontal"
        android:id="@+id/linearLayout_dummy1" android:layout_weight=".15">
    </LinearLayout>


    <LinearLayout android:layout_height="wrap_content"
        android:id="@+id/linearLayout1" android:orientation="vertical"
        android:layout_width="0dip" android:layout_weight=".7">
        <Button android:text="Button" android:id="@+id/button1"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_gravity="center">
        </Button>
        <Button android:layout_width="wrap_content" android:id="@+id/button2"
            android:layout_height="wrap_content" android:text="Button"
            android:layout_gravity="center"></Button>
        <Button android:layout_width="wrap_content" android:id="@+id/button3"
            android:layout_height="wrap_content" android:text="Button"
            android:layout_gravity="center"></Button>
    </LinearLayout>

    <LinearLayout android:layout_width="0dip"
        android:layout_height="wrap_content" android:orientation="horizontal"
        android:id="@+id/linearLayout_dummy2" android:layout_weight=".15">
    </LinearLayout>

</LinearLayout>

(1) 将 layout_width 设置为“0dip” (2) 将layout_height设置为.xx(你想要的%)

【讨论】:

  • 在阅读并尝试了很多不同的选项之后,我非常确信这是最好的答案。附言Android 布局很糟糕!不敢相信他们不接受相对 (%) 值!感谢您的回答。
  • 您确定要设置 layout_height (2) 吗?
  • 救命稻草。适用于 C# Xamarin axml 布局。
  • 宾果游戏!这对我来说就像一个冠军,让我坚持使用 LinearLayouts。
【解决方案4】:

我认为 Emiam 的做法是正确的。但也同意 Simon Veloper(Emiam 回答的评论员之一):当我们需要 70% 宽度的按钮时,我们应该为 Linearlayout 使用水平方向,并将每个 Relativelayout 的宽度设置为 0dip 并将它们的权重设置为指定 所以我建议这个版本:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:id="@+id/layoutContainer" android:orientation="horizontal">
    <RelativeLayout
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="3">
    </RelativeLayout>
    <RelativeLayout
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="14">

            //This is where you add buttons. You can make them "fill_parent".
    </RelativeLayout>
    <RelativeLayout
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="3">
    </RelativeLayout>
</LinearLayout>

【讨论】:

    【解决方案5】:

    作为 2015 年对 android 的最新更新,Google 已包含百分比支持库

    com.android.support:percent:23.1.0

    你可以参考这个网站的使用例子

    https://github.com/JulienGenoud/android-percent-support-lib-sample

    分级:

    dependencies {
        compile 'com.android.support:percent:22.2.0'
    }
    

    在布局中:

    <android.support.percent.PercentRelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <View
            android:id="@+id/top_left"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_alignParentTop="true"
            android:background="#ff44aacc"
            app:layout_heightPercent="20%"
            app:layout_widthPercent="70%" />
    
        <View
            android:id="@+id/top_right"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/top_left"
            android:background="#ffe40000"
            app:layout_heightPercent="20%"
            app:layout_widthPercent="30%" />
    
    
        <View
            android:id="@+id/bottom"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_below="@+id/top_left"
            android:background="#ff00ff22"
            app:layout_heightPercent="80%" />
    </android.support.percent.PercentRelativeLayout>
    

    【讨论】:

    • 这里强烈建议仅链接答案。请添加链接作为参考。
    【解决方案6】:

    使用新的百分比支持库

    compile 'com.android.support:percent:24.0.0'
    

    见下例

    <android.support.percent.PercentRelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
         <ImageView
             app:layout_widthPercent="50%"
             app:layout_heightPercent="50%"
             app:layout_marginTopPercent="25%"
             app:layout_marginLeftPercent="25%"/>
     </android.support.percent.PercentRelativeLayout>
    

    更多信息 Tutorial1Tutorial2Tutorial3

    【讨论】:

      【解决方案7】:

      您不能在 XML 中使用百分比来定义宽度/高度/边距/...。但是你想要使用的是“重量”属性,也就是 IMO,最相似的东西。

      另一种方法是在代码中扩展布局后以编程方式设置尺寸,方法是获取屏幕尺寸并计算所需的边距。

      【讨论】:

      • 我可以用“height”做这个吗?
      【解决方案8】:

      *您可以使用layout_weight 属性。如果要占用父宽度的 70%,则必须将子 layout_width 属性值设置为 0dp,例如 android:layout_width="0dp"*

      【讨论】:

        【解决方案9】:

        我解决了一个类似的问题,像这样对 LinearLayout 应用一些填充:

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/app_background"
            android:padding="35dip">
        
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/black">
        
        </RelativeLayout>
        </LinearLayout>
        

        这可能不会给你一个确切的百分比,但可以轻松地分级并避免额外的不必要的布局元素。

        【讨论】:

          【解决方案10】:

          Google 推出了名为 PercentRelativeLayout 的新 API

          添加编译依赖如

          编译'com.android.support:percent:22.2.0'

          因为 PercentRelativeLayout 是我们可以做的百分比布局

          【讨论】:

            猜你喜欢
            • 2012-02-05
            • 2013-07-23
            • 1970-01-01
            • 2011-01-29
            • 2014-05-16
            • 1970-01-01
            • 2013-05-22
            • 2014-02-25
            • 2017-06-11
            相关资源
            最近更新 更多