首页 > 解决方案 > 如何在嵌套视图中水平划分视图

问题描述

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/darker_gray"/>

我已经尝试了上面的代码,但没有做任何事情也尝试将高度更改为其他值,并且当使用上面的代码预览停止显示时,是否有一个好的小部件。我真的不想使用图像,因为很难将其他组件与之匹配。我也希望它相对定位。谢谢 这是下面代码的预览图片 https://i.stack.imgur.com/GD7Ro.png

<?xml version="1.0" encoding="utf-8"?>

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

    <LinearLayout
    android:baselineAligned="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            tools:context=".MainActivity">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="12dp"
                android:layout_marginTop="12dp"
                android:text="Team A"
                android:textSize="24sp" />

            <TextView
                android:id="@+id/points"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="12dp"
                android:text="0"
                android:textSize="24sp" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:onClick="on3p"
                android:text="+3 Points" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginTop="12dp"
                android:onClick="on2p"
                android:text="+2 Points" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="12dp"
                android:onClick="on1p"
                android:text="Free Throw" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="vertical"
            tools:context=".MainActivity">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="12dp"
                android:layout_marginTop="12dp"
                android:text="Team B"
                android:textSize="24sp" />

            <TextView
                android:id="@+id/points2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginBottom="12dp"
                android:text="0"
                android:textSize="24sp" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:onClick="on3pb"
                android:text="+3 Points" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="12dp"
                android:onClick="on2pb"
                android:text="+2 Points" />

            <Button
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginTop="12dp"
                android:onClick="on1pb"
                android:text="Free Throw" />

        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:text="Reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginLeft="150dp"
            android:layout_marginBottom="20dp"
            android:onClick="rst"/>


    </LinearLayout>
</RelativeLayout>

标签: androidxmlandroid-layoutandroid-studio

解决方案


<RelativeLayout>
    <linearLayout>
        <LinearLayout>
        </LinearLayout>
                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@android:color/darker_gray"/>
        <LinearLayout>
        </LinearLayout>
     </LinearLayout>
</RelativeLayout>

推荐阅读