我正在为我的简单Android应用程序进行线性布局。我想根据大小动态改变两个视图的一部分(我想拥有,从左到右的行,前20%为空,所有内容都在其余的80%内) 。对于这种方法,我选择了不同视图的权重。我为这种方法创建了一个嵌套的线性布局。例如,布局层次结构就像这样。
<linearLayout> //parent layout
<linearLayout //child 1 layout
android:layout_weight="1">
//so that this view occupy 20% of the space regardless the width of device. I intensionally wanna keep this view empty.
</linearLayout>
<linearLayout //child 2 layout
android:layout_weight="4">
//so that this view occupy 80% of the space regardless the width of device. and
//inside this view I have whatever view I wanna add on it.
<EditText>
<ImageView>
</linearLayout>
</linearLayout>
通过这种方法,Android Studio中的Lint告诉我以下警告:
这是一个嵌套布局。布局权重需要两次测量窗口小部件。当具有非零权重的LinearLayout嵌套在具有非零权重的另一个LinearLayout内时,则测量数量呈指数增长。
子级1布局无用:此LinearLayout
视图无效(无子级,无background
,无id
,无{{ 1}})
任何人都可以使用正确的布局来使布局根据设备的大小动态更改吗?我应该如何为线性布局案例正确设置空白区域?
答案 0 :(得分:2)
这是使用权重的可能解决方案:
<LinearLayout
android:layout_width="match_parent"
android:gravity="end"
android:weightSum="1">
<!-- Your content here: -->
<View
android:layout_width="0dp"
android:layout_weight="0.8"
android:layout_gravity="end" />
</LinearLayout>
答案 1 :(得分:1)
注意:您需要Percent library才能使用它。