【问题标题】:Linear Layout weights not working inside of Relative Layout线性布局权重在相对布局内不起作用
【发布时间】:2020-06-25 15:11:30
【问题描述】:

我自己和几个朋友正在为 Android 制作一款具有可玩性的游戏。在比赛场地的一部分前面,我想放置按钮。像这样:

为了将一组xml放在另一组前面,我相信我们需要使用Relative Layout。到目前为止,我们一直在内联(以编程方式)执行此操作,但现在我想使用 XML 执行此操作。我想我对权重的工作原理非常了解。我正在使用彼此内部的线性布局来做到这一点。预期的结果是让按钮只覆盖屏幕的一小部分。

不幸的是,当我将线性布局添加到相对布局时,加权功能似乎不再起作用。

为清楚起见,我添加了两行按钮,并将第二行的权重设置为大于第一行。这是布局应该是什么样子的 Android Studio 预览:

它实际上是这样出来的:

两行显示为相同大小。我希望有人可以回答我为什么会发生这种情况,如果可能的话,提供解决问题的方法。

这是我将线性布局添加到相对布局的代码:

RelativeLayout relativeLayout = (RelativeLayout)this.findViewById(R.id.testLayout);
View  view = LayoutInflater.from(this).inflate(R.layout.testgameplay_wrapper, null);
relativeLayout.addView(view);

这里是相关的 XML。我创建了一个包装类来简化事情:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
android:id="@+id/testScreenLinearWrapper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_weight=".25"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="horizontal">

    <include layout="@layout/testgameplay_buttondisplay"></include>

    </LinearLayout>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout2"
    android:layout_weight="1"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:orientation="horizontal">

    <include layout="@layout/testgameplay_buttondisplay"></include>

</LinearLayout>

编辑:

按要求测试gamplaybutton_buttondisplay.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    android:id="@+id/testScreenButtonDisplay"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/linearLayoutInnermost1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <include
                layout="@layout/defend_screen_buttons"></include>

        </LinearLayout>

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/linearLayoutInnermost2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">


            <include
                layout="@layout/test_screen_buttons"></include>

        </LinearLayout>

test_screen_buttons.xml 可能会被请求:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/testScreenButtons"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_weight=".25"
    android:layout_height="match_parent"
    android:layout_width="0dp"
    android:orientation="horizontal">
<Button
    android:id="@+id/spawnCreepButton"
    android:textColor="@android:color/white"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerInParent="true"
    android:background="@drawable/basic_button"
    android:text="Spawn Creep"
    android:onClick="RunProjectileTest"/>

</LinearLayout>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout2"
    android:layout_weight=".25"
    android:layout_height="match_parent"
    android:layout_width="0dp"
    android:orientation="horizontal">

<Button
    android:id="@+id/sendCreepsButton"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/basic_button"
    android:text="Send Creeps"
    android:onClick="StartSendCreepsScreen"/>

</LinearLayout>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout3"
    android:layout_weight=".25"
    android:layout_height="match_parent"
    android:layout_width="0dp"
    android:orientation="horizontal">
<Button
    android:id="@+id/replayButton"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/basic_button"
    android:text="Replay"
    android:onClick="StartReplayScreen"/>

</LinearLayout>

defend_screen_buttons.xml 可能会被请求:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/buttonViewGroup"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_weight=".25"
    android:layout_height="match_parent"
    android:layout_width="0dp"
    android:orientation="horizontal">

<Button
    android:id="@+id/startButton"
    android:textColor="@android:color/white"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerInParent="true"
    android:textSize="20sp"
    android:background="@drawable/basic_button"
    android:text="Start" />

    </LinearLayout>


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout2"
    android:layout_weight=".25"
    android:layout_height="match_parent"
    android:layout_width="0dp"
    android:orientation="horizontal">

<Button
    android:id="@+id/sendButton"
    android:gravity="center"
    android:textColor="@android:color/white"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@drawable/basic_button"
    android:text="Prepare Attack!" />

</LinearLayout>

现在你明白我为什么要简化它了 XD

感谢您的时间和专业知识!

【问题讨论】:

  • 发布您的testgameplay_buttondisplay
  • 我想你忘了给 android:weightSum 给 android:id="@+id/testScreenLinearWrapper" 的布局
  • 请发布您的 testgameplay_buttondisplay.xml 文件。

标签: android android-linearlayout android-relativelayout android-layout-weight


【解决方案1】:

在最外层的 LinearLayout 标签中,控制android:layout_height 属性。因为它现在设置为 match_parent,所以它占据了整个屏幕。将其设置为某个绝对数字(例如 200dp),您应该会看到差异。

【讨论】:

  • 我试图让按钮只占据屏幕的一定百分比。与屏幕分辨率(不同手机)无关的百分比。我假设 dp 是像素的测量值,但看了之后,似乎 dp 对屏幕分辨率有一定的依赖性。它似乎会根据用户使用的手机类型进行扩展。假设这一点,我认为这就是我正在寻找的答案。我得试一试,然后回复你。谢谢!
  • 看看这个链接developer.android.com/training/multiscreen/screendensities.html你应该能更好的理解。
【解决方案2】:

线性布局内的相对布局也有同样的问题,偏离中心可能是个问题。我检查了所有解决方案,但没有看到有人在layout_width 上使用match_parent 作为线性视图的孩子。将0dp 和wrap_content 更改为match_parent。这是我的问题之前和之后:

【讨论】:

  • 谢谢你真棒的陌生人。你的回答让我省了很多力气。
【解决方案3】:

我相信我找到了问题的答案。

这是我创建 LinearLayout 并将其添加到 RelativeLayout 的旧代码:

RelativeLayout relativeLayout = (RelativeLayout)this.findViewById(R.id.testLayout);
View  view = LayoutInflater.from(this).inflate(R.layout.testgameplay_wrapper, null);
relativeLayout.addView(view);

这是我的新代码:

    //Now using LinearLayout instead of View     
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.testgameplay_buttondisplay, null);

    //convert dp to pixels
    int height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, getResources().getDisplayMetrics());

    //Set the height of the layout in newly calculated pixels
    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, height);
    rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    linearLayout.setLayoutParams(rlp);

    relativeLayout.addView(linearLayout);

如您所见,我正在使用 LayoutParams 并将它们添加到我的线性布局中。这些按钮现在按照我的意愿出现在屏幕上,并且它们的大小取决于我指示的像素高度。感谢所有提供意见的人!

【讨论】:

    【解决方案4】:

    为了在子View中使用layout_weight属性,需要指定父View的weightSum。这适用于线性布局。 在下面的示例中,我声明了一个根 LinearLayout,其 weightSum 为 1,以及一个 layout_weight 等于 0.5 的子视图,这样它将占据屏幕的一半。 这个子视图(包装器)的 weightSum 为 1.5,其中有两个子视图,一个 layout_weight 等于 0.5,另一个宽度 layout_weight 等于 1。请注意,这些 LinearLayout 的 weightSum 等于 1,所以内部的每个按钮的 layout_weight 应该等于 0.2,以便均匀分布。

    希望这会有所帮助!

    <LinearLayout
        android:id="@+id/root"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">
    
        <!-- Wrapper id View fills half of the screen, 0.5 and has a weightSum of 1.5-->
        <LinearLayout
            android:id="@+id/wrapper"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5"
            android:weightSum="1.5"
            android:orientation="vertical">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight=".5"
                android:weightSum="1">
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight=".2"/>
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight=".2"/>
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight=".2"/>
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight=".2"/>
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight=".2"/>
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight=".2"/>
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight=".2"/>
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight=".2"/>
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight=".2"/>
                <Button
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight=".2"/>
            </LinearLayout>
    
        </LinearLayout>
    
    </LinearLayout>
    

    【讨论】:

    • 感谢您的回复。不幸的是,我的问题似乎源于在相对布局中使用线性布局的组合。过去,我已经成功地能够在不使用 weightsum 的情况下使用线性布局加权。我尝试用我的问题设置权重,但没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多