Android 将屏幕分成两个相等的部分,无法设置布局重量
我猜这个问题很傻,但我遗漏了一些东西,我想理解 我把屏幕分成两个相等的部分。我知道这可以通过设定体重来实现。但我不知道为什么我不能在线性布局中设置权重。相反,我设置了weightSum,但我对weightSum的理解是,在设置宽度和高度后,屏幕上留下的空间 这就是我正在做的Android 将屏幕分成两个相等的部分,无法设置布局重量,android,android-layout,android-linearlayout,Android,Android Layout,Android Linearlayout,我猜这个问题很傻,但我遗漏了一些东西,我想理解 我把屏幕分成两个相等的部分。我知道这可以通过设定体重来实现。但我不知道为什么我不能在线性布局中设置权重。相反,我设置了weightSum,但我对weightSum的理解是,在设置宽度和高度后,屏幕上留下的空间 这就是我正在做的 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.androi
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:id="@+id/main_panel">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/top"
android:weightSum="100">
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/list"
android:divider="@null"
android:dividerHeight="0dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/top"
android:weightSum="50"
android:layout_alignParentBottom="true"
android:id="@+id/ bottom">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/data"
android:orientation="vertical"
android:background="#D3D3D3"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<View
android:id="@+id/drag_line"
android:layout_width="50dp"
android:visibility="visible"
android:background="@drawable/bar"
android:layout_gravity="center_horizontal|top"
android:layout_height="170dp"
android:src="@drawable/line" />
</FrameLayout>
</LinearLayout>
</RelativeLayout>
PS:我无法使用android棒棒糖设置布局权重属性,请尝试以下代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#ffffff"
android:layout_height="match_parent"
android:id="@+id/main_panel">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:id="@+id/top"
>
<ListView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/list"
android:divider="@null"
android:dividerHeight="0dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_below="@id/top"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:id="@+id/ bottom">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/data"
android:orientation="vertical"
android:background="#D3D3D3"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<View
android:id="@+id/drag_line"
android:layout_width="50dp"
android:visibility="visible"
android:background="@drawable/bar"
android:layout_gravity="center_horizontal|top"
android:layout_height="170dp"
android:src="@drawable/line" />
</FrameLayout>
</LinearLayout>
</RelativeLayout>
问题:
不能在仅限于linearlayout布局参数本身的相对布局中添加权重和
解决方案:
在relatie布局中添加另一个linearlayout作为两个内部线性布局的父级
样本:
Weightsum是父级用于查找其子级的总权重和的属性。此属性仅适用于。其子女应指定其体重,宽度或高度为0dp。你可以找到教程。因此,您的layout.xml将
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_panel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:weightSum="100" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="50"
android:divider="@null"
android:dividerHeight="0dp" />
<FrameLayout
android:id="@+id/data"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:background="#D3D3D3"
android:orientation="vertical" >
<View
android:id="@+id/drag_line"
android:layout_width="50dp"
android:layout_height="170dp"
android:layout_gravity="center_horizontal|top"
android:visibility="visible" />
</FrameLayout>
</LinearLayout>
你想要哪两部分?一边是listview,另一边是什么?第一个是带有listview的linerlayout,第二个是包含FrameLayout的linerlayout检查我下面的答案,它会起作用。使用linearlayout而不是Relative,你可以使用Weights。它不是分割,只有顶部是visibleno,但为什么要给宽度0dp,我需要两个垂直方向,所以如果我将Orientation设置为垂直方向,我不能为height@user2323你想要垂直的还是水平的?我做这件事是为了钱。请提及正确。对不起,我没有提及,我想要垂直,但要使其垂直,我只需要将方向和高度设置为0dp…我在这里正确吗?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_panel"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:weightSum="100" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="50"
android:divider="@null"
android:dividerHeight="0dp" />
<FrameLayout
android:id="@+id/data"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:background="#D3D3D3"
android:orientation="vertical" >
<View
android:id="@+id/drag_line"
android:layout_width="50dp"
android:layout_height="170dp"
android:layout_gravity="center_horizontal|top"
android:visibility="visible" />
</FrameLayout>
</LinearLayout>
Get Root attribute always be <Relative> and with in that write your remaining XML code.
e.g.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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:background="@color/black">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white">
</LinearLayout>
</LinearLayout>
//Replace above XML code by...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
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:background="@color/black">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/white">
</LinearLayout>
</LinearLayout>
</RelativeLayout>