我有一个布局xml文件,其中包含带有2个子LinearLayouts的LinearLayout。我想分开孩子,所以我将它们放在2个布局xml文件中,然后在Kotlin代码中定义父级LinearLayout并将孩子添加到其中。如果这是正确的方法,请纠正我。
问题是,第一个LinearLayout子项的权重为0.2,第二个子项的权重为0.8。因此,当我将它们添加到Kotlin中定义的父级LinearLayout时,它将返回一个空项目。
这是我要分离的文件-profile_item.xml:
<?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="wrap_content"
android:paddingBottom="12dp"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:background="#000"
android:orientation="vertical"
>
<TextView
android:id="@+id/dayNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold"
/>
<TextView
android:id="@+id/dayName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sun"
android:textColor="#fff"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/numberBar"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:background="@drawable/rounded_corner"
>
<TextView
android:id="@+id/numberBarText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="12dp"
android:layout_marginLeft="12dp"
android:gravity="center_vertical"
android:text="6"
android:textColor="#fff"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
您的父级布局没有任何方向,因此请添加方向
您的父级布局应该看起来像这样
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="12dp"
>
如果您想让孩子并排查看然后设置方向
android:orientation="horizontal"