Layout_weight无法正常工作

时间:2016-02-23 05:24:24

标签: android xml android-layout android-studio android-layout-weight

我有跟随xml,我想要imageview,然后是两个textview,然后再次查看imageview,但它无法正常工作。

这是我打算拥有的:

this

这是我得到的:

enter image description here

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

12 个答案:

答案 0 :(得分:2)

insidelaylayout必须有0宽度

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

答案 1 :(得分:1)

这是因为宽度使它0dp ..copy它完美的波纹管代码

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

答案 2 :(得分:1)

你需要对图像和textview使用 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>

答案 3 :(得分:1)

内部线性布局第二次机会遵循代码。

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

答案 4 :(得分:1)

我更改了您的代码检查

更改图片根据需要查看并使用此

替换您的代码
<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>

答案 5 :(得分:1)

您可以使用以下代码: -

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

答案 6 :(得分:1)

根本不要使用权重,然后根据要求为所有主要观点分配权重。它肯定会有效。为什么要把事情弄复杂。我已经为你做了。请在下面查看。

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

答案 7 :(得分:1)

这是使用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>

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

答案 8 :(得分:1)

我已经拿走了你的代码并将其剥离了。以下,至少在工作室设计中,具有您所描述的布局。也许逐步改变/适应这一点;检查时,它会保持所需的布局。

<?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"之类的问题,这可能是一个问题,作为一个例子。

答案 9 :(得分:1)

根据Image。

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

答案 10 :(得分:1)

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

答案 11 :(得分:0)

使用可以跟随重量的东西..我认为这个答案对你有帮助.. xml image     这个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>