线性布局权重在相对布局内不起作用

时间:2015-09-25 00:11:22

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

我和一些朋友正在制作一款具有游戏领域的Android游戏。在比赛场地的一部分前面,我想放置按钮。像这样:

enter image description here

为了将一组xml放在另一组xml之前,我相信我们需要使用相对布局。到目前为止,我们一直在内联(以编程方式),但现在我想使用XML来做到这一点。我想我对加权的工作方式有很好的理解。我这样做是使用彼此内部的线性布局。预期的结果是按钮仅覆盖屏幕的一小部分。

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

为清楚起见,我添加了两行按钮,并将第二行加权为大于第一行。以下是布局应如下所示的Android Studio预览:

enter image description here

以下是实际出现的内容:

enter image description here

这两行显示为相同的大小。我希望有人可以给我一个答案,说明为什么会这样,如果可能的话,还有一个解决方案来解决问题。

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

* {
    -webkit-touch-callout: none;
     -webkit-user-select: none; /* Disable selection/copy in UIWebView */
}

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

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

编辑:

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

可能会请求

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

可能会要求

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

现在你明白为什么我试图简化它XD

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

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

这是我的旧代码创建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并将它们添加到我的linearLayout中。按钮现在出现在我想要的屏幕上,它们的大小取决于我指示的像素高度。感谢所有提出意见的人!

答案 2 :(得分:0)

在线性布局内,相对布局存在相同的问题,与中心的偏离可能是一个问题。我检查了所有解决方案,但没有看到有人在match_parent上使用layout_width作为线性视图的子级。将0dpwrap_content更改为match_parent。这是我遇到的问题之前和之后的问题:enter image description here

答案 3 :(得分:-1)

要在子视图中使用layout_weight属性,您需要指定父视图的weightSum。这适用于LinearLayouts。 在下面的示例中,我已经声明了一个根LinearLayout,其weightSum为1,并且子视图的layout_weight等于.5,因此这将填充屏幕的一半。 这个子视图(包装器)的weightSum为1.5,其中有两个子视图,一个layout_weight等于.5,另一个宽度一个layout_weight等于1.注意这些LinearLayouts的weightSum等于1,所以内部的每个按钮都应该有一个layout_weight等于.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>
相关问题