【问题标题】:Android layout correct in designer but wrong in deviceAndroid 布局在设计器中正确,但在设备中错误
【发布时间】: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。

标签: android xml layout


【解决方案1】:

在 ListView 中使用 android:layout_weight="1" 代替 android:layout_weight="90"。另外 android:layout_height="wrap_content" 代替 android:layout_height="0dp" 并且不要在 LinearLayout 中使用 layout_weight。您可以在 EditText 中使用 layout_weight="1" 。 希望这能解决您的问题:)

【讨论】:

  • 这背后的逻辑在哪里?正如我所说,我需要从逻辑上知道问题出在哪里。
  • 您在 ListView 和 LinearLayout 中使用 layout_weight="90" & layout_weight="10",因此它将设备高度的 90 部分用于列表视图,将设备高度的 10 部分用于线性布局。为此,它在不同设备中的行为会有所不同。
  • 我认为你错了。在所有设备中都应遵守此规则。
【解决方案2】:

试试这个...您只是在父线性布局中错过了一行 weightsum。 添加这个。

android:weightSum="100"

所以你的最终代码看起来像

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="100">

       <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="wrap_content"
            android:layout_height="match_parent"
            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>

【讨论】:

  • 我记得我之前设置了 weightSum,我不认为这是问题所在。在设计器中布局是正确的。
  • 会提供截图吗?只需提供链接
  • 我不在,我一回到家就提供一个。感谢您尝试帮助我。
  • 你确定我也在等待您的回复。
  • 我错了,weightSum 没有解决问题,打开键盘后它看起来不错
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多