繁体   English   中英

Android:使用权重时LinearLayout出现在屏幕外

[英]Android: LinearLayout going out of screen when using weight

为什么在以下xml文件中将第3个线性布局(包含两个文本视图)推出屏幕? 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>

我正在使用Fragment(Tabbed Activity)。 在活动中使用相同的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"/>

    <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>

与其使用嵌套权重(这对性能非常不利),不如使用ConstraintLayout ,它在布局编辑器中非常容易实现。

如果您从未使用过它,可能看起来有点复杂; 但事实并非如此。 请参考此链接以获取有关如何实现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>

找到了! 问题不在于片段布局,而在于其容器,即ViewPager。 我正在使用“选项卡式活动”,默认情况下,“查看寻呼机”过长(超出屏幕范围)。 解决方法如下: 屏幕下方的“ Android选项卡式活动”

谢谢你们的努力。

Hiiii我认为您没有适当的信息,如何使用“ Weightsum”。 很好,没有问题,我将向您解释,当您要在线性布局中使用“ Weight”时,应在主“ LinearLayout”中使用“ 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM