Layout_weight 无法正常工作

Layout_weight does not work properly

我有以下 xml 我想要图像视图,然后是两个文本视图,然后是图像视图,但它不能正常工作。

这是我的计划:

这是我得到的:

<LinearLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_gravity="center_horizontal"
     android:orientation="horizontal"
     android:clickable="true"
     android:onClick="userProfileDetailAction"
     android:id="@+id/userProfileDetail"
     android:paddingTop="5dp"
     android:paddingBottom="5dp">

            <com.example.eyeniaras.satdostum.CircleImageView
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_weight="0.3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:background="@drawable/circle_shape"
                app:civ_border_width="0.2dp"
                app:civ_border_color="@color/dark"
                android:id="@+id/userImage"
                android:scaleType="centerCrop"/>

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.6">

                <TextView
                    android:id="@+id/userName"
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_gravity="center"
                    android:textColor="@android:color/black"
                    android:background="#ffffff"
                    android:textSize="18sp"
                    android:layout_weight="1"/>

                <TextView
                    android:id="@+id/userRegistrationDate"
                    android:layout_width="wrap_content"
                    android:layout_height="0dp"
                    android:layout_gravity="center"
                    android:textColor="@android:color/black"
                    android:background="#ffffff"
                    android:textSize="18sp"
                    android:text = "45 days ago"
                    android:layout_weight="1"/>
            </LinearLayout>

            <ImageView
                android:id="@+id/userClick"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_weight="0.1"
                android:background="@drawable/list_arrow" />

 </LinearLayout>

内部线性布局必须有 0 宽度

        <LinearLayout
                android:orientation="vertical"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:weightSum="100"
                android:layout_weight="60">

这是因为宽度使它成为 0dp..复制下面的代码它完美无缺

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:orientation="horizontal"
        android:clickable="true"
        android:weightSum="100"
        android:onClick="userProfileDetailAction"
        android:id="@+id/userProfileDetail"
        android:paddingTop="5dp"
        android:paddingBottom="5dp">

                      <com.example.eyeniaras.satdostum.CircleImageView
                        xmlns:app="http://schemas.android.com/apk/res-auto"
                        android:layout_weight="20"
                        android:layout_width="0dp"
                        android:layout_height="10dp"
                        android:background="@drawable/circle_shape"
                        app:civ_border_width="0.2dp"
                        app:civ_border_color="@color/dark"
                        android:id="@+id/userImage"
                        android:scaleType="centerCrop"/>

                       <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="0dp"
android:orientation="vertical"
                        android:layout_height="match_parent"
                        android:weightSum="100"
                        android:layout_weight="60">

                           <TextView
                               android:id="@+id/userName"
                               android:layout_width="wrap_content"
                               android:layout_height="0dp"
                               android:layout_gravity="center"
                               android:textColor="@android:color/black"
                               android:background="#ffffff"
                               android:textSize="18sp"
                               android:textStyle="bold"
                               android:layout_weight="50"/>

                           <TextView
                               android:id="@+id/userRegistrationDate"
                               android:layout_width="wrap_content"
                               android:layout_height="0dp"
                               android:layout_gravity="center"
                               android:textColor="@android:color/black"
                               android:background="#ffffff"
                               android:textSize="18sp"
                               android:text = "45 days ago"
                               android:textStyle="bold"
                               android:layout_weight="50"/>
                        </LinearLayout>

                      <ImageView
                        android:id="@+id/userClick"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:textColor="@android:color/black"
                        android:textSize="18sp"
                        android:textStyle="bold"
                        android:layout_weight="20"
                        android:background="@drawable/list_arrow" />

     </LinearLayout>

你需要对图像和文本视图使用 android:layout_weight 所以这是解决方案,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/userProfileDetail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_margin="5dp"
    android:clickable="true"
    android:onClick="userProfileDetailAction"
    android:orientation="horizontal"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:weightSum="5">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1.4">

        <com.example.eyeniaras.satdostum.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/userImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/circle_shape"
            android:scaleType="centerCrop"
            app:civ_border_color="@color/dark"
            app:civ_border_width="0.2dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical"
        android:weightSum="2">

        <TextView
            android:id="@+id/userName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="#ffffff"
            android:gravity="center"
            android:text="Hi"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/userRegistrationDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="#ffffff"
            android:gravity="center"
            android:text="45 days ago"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.6">

        <ImageView
            android:id="@+id/userClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="20"
            android:background="@drawable/list_arrow"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>

inside linearlayout second chance follow code.

            <LinearLayout
            android:orientation="vertical"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="60">

我改了你的代码检查一下

根据需要更改图像视图并用此替换代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:clickable="true"
android:weightSum="100"
android:onClick="userProfileDetailAction"
android:id="@+id/userProfileDetail"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<ImageView
    android:layout_weight="20"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:id="@+id/userImage"
    android:src="@android:drawable/btn_star_big_on"
    android:scaleType="centerCrop"/>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:weightSum="100"
    android:gravity="center_vertical"
    android:layout_weight="60">

    <TextView
        android:id="@+id/userName"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center" android:text="hey hi"
        android:textColor="@android:color/black"
        android:background="#ffffff"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_weight="50"/>

    <TextView
        android:id="@+id/userRegistrationDate"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:textColor="@android:color/black"
        android:background="#ffffff"
        android:textSize="18sp"
        android:text = "45 days ago"
        android:textStyle="bold"
        android:layout_weight="50"/>
</LinearLayout>

<ImageView
    android:id="@+id/userClick"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:src="@android:drawable/btn_star"
    android:layout_weight="20"  /></LinearLayout>

您可以使用以下代码:-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal"
    android:weightSum="4" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:orientation="vertical"
        android:weightSum="1" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".5"
            android:gravity="center"
            android:text="hello" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight=".5"
            android:gravity="center"
            android:text="hello" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>
</LinearLayout>

根本不用weightsum然后按要求给所有main赋权views.It肯定会work.why让事情complex.I已经做到了you.Check ] 它在下面。

  <LinearLayout
    android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:clickable="true"
  android:onClick="userProfileDetailAction"
android:id="@+id/userProfileDetail"
android:paddingTop="5dp"
android:paddingBottom="5dp">

              <com.example.eyeniaras.satdostum.CircleImageView
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:layout_weight="0.2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:background="@drawable/circle_shape"
                app:civ_border_width="0.2dp"
                app:civ_border_color="@color/dark"
                android:id="@+id/userImage"
                android:scaleType="centerCrop"/>

               <LinearLayout
                android:orientation="vertical"
                android:layout_width="0dp"
                android:layout_height="match_parent"

                android:layout_weight="0.6">

                   <TextView
                       android:id="@+id/userName"
                       android:layout_width="wrap_content"
                       android:layout_height="0dp"
                       android:layout_gravity="center"
                       android:textColor="@android:color/black"
                       android:background="#ffffff"
                       android:textSize="18sp"
                       android:textStyle="bold"
                       android:layout_weight="1"/>

                   <TextView
                       android:id="@+id/userRegistrationDate"
                       android:layout_width="wrap_content"
                       android:layout_height="0dp"
                       android:layout_gravity="center"
                       android:textColor="@android:color/black"
                       android:background="#ffffff"
                       android:textSize="18sp"
                       android:text = "45 days ago"
                       android:textStyle="bold"
                       android:layout_weight="1"/>
                </LinearLayout>

              <ImageView
                android:id="@+id/userClick"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:textColor="@android:color/black"
                android:textSize="18sp"
                android:textStyle="bold"
                android:layout_weight="0.2"
                android:background="@drawable/list_arrow" />

      </LinearLayout>

这是使用 PercentageRelativeLayout 的完美场景。这种布局的最大优点是您可以使用基于百分比的尺寸,这非常简单。

这是一个简单的例子,

<android.support.percent.PercentRelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
     <ImageView
         app:layout_widthPercent="50%"
         app:layout_heightPercent="50%"
         app:layout_marginTopPercent="25%"
         app:layout_marginLeftPercent="25%"/>
 </android.support.percent.PercentFrameLayout>

这使用起来非常简单直观,也针对各种用例进行了优化。

我已经获取了您的代码并将其剥离。以下至少在工作室设计中具有您所描述的布局。也许逐步 change/adapt 这个;在进行过程中检查它是否保持了所需的布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res-auto"
    android:layout_gravity="center_horizontal"
    android:orientation="horizontal"
    android:clickable="true"
    android:weightSum="100"
    android:onClick="userProfileDetailAction"
    android:id="@+id/userProfileDetail"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

  <ImageView
      android:layout_width="100dp"
      android:layout_height="100dp" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="500dp"
        android:layout_height="100dp"
        android:weightSum="100"
        android:layout_weight="60">

        <TextView
            android:id="@+id/userName"
            android:layout_width="500dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:textColor="@android:color/black"
            android:background="#ffffff"
            android:textSize="18sp"
            android:textStyle="bold"
            android:layout_weight="50"/>

        <TextView
            android:id="@+id/userRegistrationDate"
            android:layout_width="500dp"
            android:layout_height="50dp"
            android:layout_gravity="center"
            android:textColor="@android:color/black"
            android:background="#ffffff"
            android:textSize="18sp"
            android:text = "45 days ago"
            android:textStyle="bold"
            android:layout_weight="50"/>
    </LinearLayout>

    <ImageView
        android:id="@+id/userClick"
        android:layout_width="100dp"
        android:layout_height="100dp" />

</LinearLayout>

请注意,我已经取消了 width="match_parent" 之类的东西,举个例子,这可能是个问题。

根据图像。


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/userProfileDetail"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:clickable="true"
    android:onClick="userProfileDetailAction"
    android:orientation="horizontal"
    android:padding="5dp"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:weightSum="5">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <com.example.eyeniaras.satdostum.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/userImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/circle_shape"
            android:scaleType="centerCrop"
            app:civ_border_color="@color/dark"
            app:civ_border_width="0.2dp" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="3"
        android:orientation="vertical"
        android:weightSum="2">

        <TextView
            android:id="@+id/userName"
            android:layout_width="match_parent"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="#ffffff"
            android:text="45 days ago"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/userRegistrationDate"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="#ffffff"
            android:gravity="center"
            android:text="45 days ago"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1">

        <ImageView
            android:id="@+id/userClick"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="20"
            android:background="@drawable/list_arrow"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>
<LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:weightSum="100"
                android:layout_weight="60">

android:layout_width="match_parent",这个布局的宽度是match_parent,所以它把其他的都填满了,改成wrap_content,就像

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/userProfileDetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:clickable="true"
android:onClick="userProfileDetailAction"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:weightSum="100" >

<ImageView  
    android:id="@+id/userImage"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:layout_weight="20"
    android:background="@drawable/ic_launcher"
    android:scaleType="centerCrop"
    />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:weightSum="100"
    android:layout_weight="60"
    android:orientation="vertical"
    >

    <TextView
        android:id="@+id/userName"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="50"
        android:background="#ffffff"
        android:text="46 days ago"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/userRegistrationDate"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_weight="50"
        android:background="#ffffff"
        android:text="45 days ago"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:textStyle="bold" />
</LinearLayout>

<ImageView  
    android:id="@+id/userImage"
    android:layout_width="0dp"
    android:layout_height="40dp"
    android:layout_weight="20"
    android:background="@drawable/ic_launcher"
    android:scaleType="centerCrop"
    />

</LinearLayout>

使用可以跟随的东西来减肥..我认为这个答案对你有帮助.. 这个 xml

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="30"
        android:background="@drawable/background"
        android:gravity="center" >

        <ImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@drawable/images" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="40" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="10" >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="5"
                android:background="@drawable/background"
                android:gravity="center" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Firstname" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="5"
                android:background="@drawable/background"
                android:gravity="center" >
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Lastname" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="30"
        android:background="@drawable/background"
        android:gravity="center" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/download" />
    </LinearLayout>
</LinearLayout>

and this code for background past in drawable/background
<?xml version="1.0" encoding="UTF-8"?>

<!-- res/drawable/rounded_edittext.xml -->

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="12dp"
    android:shape="rectangle" >
    <solid android:color="#FFFFFF" />
    <stroke
        android:width="0.3dp"
        android:color="#797979" />`enter code here`
</shape>