android – 为LinearLayout定义百分比宽度?

android – 为LinearLayout定义百分比宽度?,第1张

概述参见英文答案 > Set ImageView’s max width as a percent of its parent’s width                                    1个 我想为包含一些按钮的LinearLayout定义百分比宽度(70%),以便我可以将其居中,以便子按钮可以fill_parent.这是一张显示我的意思的图片: 我目前的布局如下: <?x 参见英文答案 > Set ImageView’s max width as a percent of its parent’s width                                    1个
我想为包含一些按钮的linearLayout定义百分比宽度(70%),以便我可以将其居中,以便子按钮可以fill_parent.这是一张显示我的意思的图片:

我目前的布局如下:

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

我引用的linearLayout具有ID:linearLayout1.我该怎么做呢?

解决方法 您必须设置元素的权重属性.为linearLayout创建三个relativeLayouts作为子项,并设置权重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指出权重可以是小数而不是整数.

总结

以上是内存溢出为你收集整理的android – 为LinearLayout定义百分比宽度?全部内容,希望文章能够帮你解决android – 为LinearLayout定义百分比宽度?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1121445.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存