【问题标题】:Android: LinearLayout going out of screen when using weightAndroid:使用权重时LinearLayout走出屏幕
【发布时间】: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


【解决方案1】:

为避免嵌套权重(不利于性能),您可以使用相对布局。

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

    <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:orientation="horizontal"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

【讨论】:

  • 你能在监控工具中看到版面的高度吗?您可以使用 android monitor 检查布局的实际放置方式。如果你发现一些有趣的东西,请发布它。
  • 它无法显示预期的行为。您需要从设备中捕获视图层次结构。它在监控工具上看起来完全一样。
【解决方案2】:

不要使用嵌套权重(恰好是非常糟糕的bad for performance),而是使用在布局编辑器中很容易实现的ConstraintLayout

如果您以前从未使用过它,它可能会显得有点复杂;但事实并非如此。请参阅此 link 以了解如何实现 ConstraintLayout

这是你想要实现的代码

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <android.support.constraint.Guideline
        android:id="@+id/guideline1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.1"
        tools:layout_editor_absoluteY="51dp"
        tools:layout_editor_absoluteX="0dp" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.9"
        tools:layout_editor_absoluteY="460dp"
        tools:layout_editor_absoluteX="0dp" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="192dp" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.75"
        tools:layout_editor_absoluteY="0dp"
        tools:layout_editor_absoluteX="288dp" />

    <TextView
        android:id="@+id/stoneTV"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:text="@string/stoneText"
        app:layout_constraintBaseline_toBaselineOf="@+id/resetButton"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/guideline4"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />

    <Button
        android:id="@+id/resetButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:text="@string/reset_text"
        app:layout_constraintBottom_toTopOf="@+id/guideline1"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="@+id/guideline4"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.64"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="24dp" />

    <TextView
        android:id="@+id/playerTV"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/player_text"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="16dp"
        app:layout_constraintTop_toTopOf="@+id/guideline2"
        android:layout_marginTop="8dp"
        app:layout_constraintRight_toLeftOf="@+id/guideline3"
        android:layout_marginRight="8dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintHorizontal_bias="0.0" />

    <TextView
        android:id="@+id/computerTV"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/computer_text"
        app:layout_constraintTop_toTopOf="@+id/guideline2"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="16dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="@+id/guideline3"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp" />

    <FrameLayout
        android:id="@+id/contentLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginLeft="8dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/guideline2"
        app:layout_constraintTop_toTopOf="@+id/guideline1"
        android:layout_marginTop="8dp">

        <!--Add your other view(s) here-->

    </FrameLayout>
</android.support.constraint.ConstraintLayout>

【讨论】:

  • 谢谢。我删除了嵌套重量。请参阅我编辑的问题。即使它不起作用。 ConstraintLayout 将是最后一个选项。
【解决方案3】:

找到了!问题不在于片段布局,而在于它的容器,即 ViewPager。我正在使用选项卡式活动,默认情况下 View Pager 太长(走出屏幕)。 这是解决方案:Android Tabbed Activity Bottom off Screen

感谢大家的努力。

【讨论】:

    【解决方案4】:

    嗨iii 我认为您没有正确的信息,如何使用“Weightsum”。 没问题,我会解释你的, 当您想在线性布局中使用“权重”时,您应该在主“线性布局”中使用“Weightsum”,然后您可以使用子布局或小部件的权重总和,如下面的代码。

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1"
        android:orientation="horizontal">
    
        <TextView
            android:id="@+id/messageTextView"
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:layout_weight="0.5"
            android:text="Place a stone" />
    
        <Button
            android:id="@+id/resetButton"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:layout_width="0dp"
            android:text="Reset" />
    
    </LinearLayout>
    

    【讨论】:

    • WeightSum 是可选的。即使添加 weightSum,它也不起作用。
    • 请上传你的新xml,我会给你更好的答案。
    猜你喜欢
    • 2016-01-02
    • 2023-03-27
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    相关资源
    最近更新 更多