Android - layout_weight not working

送分小仙女□ 提交于 2019-12-12 06:44:14

问题


Here is the XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:minHeight="40dp" >

    <LinearLayout
        android:id="@+id/contentLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/separatorView"
        android:weightSum="3"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/itemTitleLabel"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:gravity="right"
            android:lines="0"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="5dp"
            android:textColor="@android:color/black"
            android:textSize="16sp"
            android:textStyle="bold" />

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:background="@android:color/white" >

            <TextView
                android:id="@+id/itemDetailsLabel"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:background="@null"
                android:gravity="left"
                android:lines="0"
                android:paddingBottom="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:paddingTop="5dp"
                android:textColor="@android:color/black"
                android:textSize="16sp"
                android:textStyle="normal" />

            <ImageView
                android:id="@+id/itemAvailabilityImageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:background="@null"
                android:paddingBottom="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:paddingTop="5dp"
                android:scaleType="centerInside" />
        </RelativeLayout>
    </LinearLayout>

    <View
        android:id="@+id/separatorView"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true"
        android:background="@color/tab_bar_separator" />

</RelativeLayout>

This is a row layout in a list view. What's shown is the last view only. The one with the id = separatorView.

Update:


回答1:


You should specify in your last View element (The separator)

android:layout_below="@id/ContentLayout"

To make sure the separatoe won't be aligned to the top of the RelativeLayout.




回答2:


Hope this helps,if i got your question right:

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="#000000" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Keyboard" />

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

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="#000000" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Flash" />

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

<View
    android:layout_width="match_parent"
    android:layout_height="2dp"
    android:background="#000000" />



来源:https://stackoverflow.com/questions/20060964/android-layout-weight-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!