【发布时间】:2017-08-21 18:36:11
【问题描述】:
为什么第三个线性布局(包含两个文本视图)在以下 xml 文件中被推出屏幕? Android Studio 中的同步设计显示了预期的行为,但在手机中进行测试时,第 3 线性布局部分可见。
我想要实现的是将屏幕分成 3 个部分(10%、80% 和 10%)。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/messageTextView"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="3"
android:text="Place a stone" />
<Button
android:id="@+id/resetButton"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_width="0dp"
android:text="Reset" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8">
<me.varunon9.fito.CanvasBoardView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/userInfoTextView"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="@drawable/focussed_background"
android:text="Your turn\nStones left: 9" />
<TextView
android:id="@+id/computerInfoTextView"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="Computer's turn\nStones left: 9" />
</LinearLayout>
甚至这个xml也被推出屏幕-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/topBar"
android:orientation="horizontal"
android:layout_alignParentTop="true">
<TextView
android:id="@+id/messageTextView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="Place a stone" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp"
android:layout_marginBottom="40dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="@+id/bottomBar"
android:layout_marginBottom="0dp"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<TextView
android:id="@+id/userInfoTextView"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:text="User Info" />
</LinearLayout>
</RelativeLayout>
我正在使用片段(选项卡式活动)。在活动中使用相同的 xml 布局(高于 sn-ps)时,一切正常。片段似乎有问题。
【问题讨论】:
-
您是否尝试过对所有父级布局使用 weightSum?
-
另外,强烈建议不要使用嵌套权重。这对性能不利。
-
将 android:weightSum="10" 添加到父布局不起作用。有什么建议可以实现所需的行为(将屏幕按比例高度划分)?
-
为什么不使用相对布局并适当对齐。例如:设置底栏和顶栏固定高度,分别设置alignParentBottom和alignPatentTop,然后使用底部边距=底部栏高度和顶部边距=顶部栏高度的中心画布。
-
好的,谢谢。我试试看。
标签: android android-layout android-linearlayout android-layout-weight