【发布时间】:2017-02-09 08:25:03
【问题描述】:
我在 android 中遇到了一个奇怪的问题。我在 ListView 下方有一个 LinearLayout。列表视图的宽度为 0dp,layout_weight 为 90。线性布局 layout_weight 为 10。高度为 0dp。在设备上,线性布局消失了。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView android:id="@+id/chatsListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="90"
android:divider="@null"
android:dividerHeight="0dp"
android:listSelector="@android:color/transparent"
android:transcriptMode="alwaysScroll"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:padding="10dp">
<EditText
android:id="@+id/messageEditText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:inputType="textMultiLine"
android:hint="type message here"
android:ems="10"/>
<ImageButton
android:id="@+id/sendMessageButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@drawable/send_button"/>
</LinearLayout>
</LinearLayout>
我需要知道上面的问题是什么,我不想以另一种方式来做。我做这个项目是为了逻辑学习,而不是复制粘贴。
在这里预览设计师:
这是设备中的预览:
当我打开和关闭键盘时,它会像在设计器中一样调整大小。我需要知道代码是否有任何错误。
编辑:来自 android-dev IRC 的 Zharf 找到了这个解决方案:
Viewpager is being pushed out of the screen [CoordinatorLayout] [Design Library]
【问题讨论】:
-
如果您只使用 PercentRelativeLayout,您可以摆脱多余的、不必要的 LinearLayout。
-
请尝试将
android:weightSum="100"添加到外部LinearLayout。 -
您能否提供“设计器”和“实际”设备中设备的详细信息?比如设备的尺寸和设备本身是什么(nexus、三星等)。
-
该设备是nexus,我已经在设计器中测试了许多设备,它看起来不错。但在模拟器中,我认为它只是关闭了屏幕。我在父级中的 weightsum 之前设置为一个,另一个布局权重为 0.9 和 0.1。发生了同样的问题。
-
@Slav 这完全没有必要。 Android 会自动计算 weightSum。