Android Layout_Weight 属性 不工作

Android Layout_Weight property not working

出于某种原因,布局 "lefttop" 和 "topright" 在我的片段中保持为 0dp。

我已经检查过,当我为任一相对布局的宽度提供实际值 (id: 50dp) 时,其中的项目实际开始 appearing.I 还确保 [=22= 的父相对布局] 和 "topright" 实际上确实占用了我的 cardview 的整个宽度和高度。

我实现 weightSum 和 layout_weight 的方式有问题吗?我查看了人们遇到的许多不同的类似问题

这是我的代码:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    card_view:cardCornerRadius="4dp"
    android:layout_margin="5dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum = "1.5">

        <!--Top Left section (name and adress)-->
        <RelativeLayout
            android:id="@+id/lefttop"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight = "1"
            android:background="@drawable/borderright">

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:text="contact det"
                android:gravity="center_vertical"
                android:textColor="@android:color/black"
                android:paddingLeft="5dp"
                android:textSize="40dp"
                android:layout_alignParentTop="true"/>

            <TextView
                android:id="@+id/txtName"
                android:layout_width="wrap_content"
                android:layout_height="10dp"
                android:text="Name"
                android:gravity="center_vertical"
                android:textSize="10dp"
                android:layout_below="@id/title"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="0dp"
                android:paddingLeft="15dp"/>
        </RelativeLayout>


        <!--top right section with type of job and company-->
       <RelativeLayout
        android:id="@+id/topright"
        android:layout_toRightOf="@+id/lefttop"
        android:layout_width="0dp"
        android:layout_height="26dp"
        android:layout_weight = "0.5"
        android:background="@drawable/borderright">


               <TextView
                   android:id="@+id/txtSurname"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:text="Surname"
                   android:gravity="center_vertical"
                   android:textSize="10dp"
                   android:layout_alignParentTop="true"
                   android:layout_marginTop="10dp"
                   android:layout_marginLeft="5dp"
                   android:background="@drawable/borderdown"/>

           <TextView
               android:id="@+id/txtEmail"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="Email"
               android:textSize="10dp"
               android:layout_marginTop="10dp"
               android:layout_marginLeft="5dp"/>

           </RelativeLayout>




    </RelativeLayout>
    </android.support.v7.widget.CardView>

layout_weight只适用于LinearLayout..... 这是您的解决方案......

 <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

    card_view:cardCornerRadius="4dp"
    android:layout_margin="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum = "1.5">

        <!--Top Left section (name and adress)-->
        <LinearLayout
            android:id="@+id/lefttop"
            android:layout_width="50dp"
            android:layout_height="0dp"
            android:layout_weight = "1"
            android:background="@drawable/borderright">

            <TextView
                android:id="@+id/title"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:text="contact det"
                android:gravity="center_vertical"
                android:textColor="@android:color/black"
                android:paddingLeft="5dp"
                android:textSize="40dp"
                android:layout_alignParentTop="true"/>

            <TextView
                android:id="@+id/txtName"
                android:layout_width="wrap_content"
                android:layout_height="10dp"
                android:text="Name"
                android:gravity="center_vertical"
                android:textSize="10dp"
                android:layout_below="@id/title"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="0dp"
                android:paddingLeft="15dp"/>
        </LinearLayout>


        <!--top right section with type of job and company-->
       <LinearLayout
        android:id="@+id/topright"
        android:layout_toRightOf="@+id/lefttop"
        android:layout_width="26dp"
        android:layout_height="0dp"
        android:layout_weight = "0.5"
        android:background="@drawable/borderright">


            <TextView
                android:id="@+id/txtSurname"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Surname"
                android:gravity="center_vertical"
                android:textSize="10dp"
                android:layout_alignParentTop="true"
                android:layout_marginTop="10dp"
                android:layout_marginLeft="5dp"
                android:background="@drawable/borderdown"/>

           <TextView
               android:id="@+id/txtEmail"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="Email"
               android:textSize="10dp"
               android:layout_marginTop="10dp"
               android:layout_marginLeft="5dp"/>

           </LinearLayout>

    </LinearLayout>
    </android.support.v7.widget.CardView>