简体   繁体   中英

ImageView doesn't show inside LinearLayout with layer-list as background

I have the following layer-list

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >


    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <solid android:color="#FF000000"/>
            <stroke android:width="0dp" android:color="#FF000000" />
            <corners android:topLeftRadius="10dp" android:topRightRadius="10dp"/>
            <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
        </shape>
    </item>

</layer-list>

This resource is a black background with some rounded border (Top left and top Right). I want put it in LinearLayout as background property and inside LinearLayour I want put a white Textview and a ImageView. I have the following code

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/ll"
            android:orientation="horizontal"
            android:background="@drawable/profile_background_head_item">

            <TextView
                android:id="@+id/txt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/origin"
                android:gravity="center"
                android:textSize="20sp"
                android:textColor="#FFFFFFFF"
                />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_config"/>
            </LinearLayout>

The problem is that ImageView doesn't show, I think that it is due that my layer-list code, but I don't know how to solve it.

Any ideas?

Thanks

Your TextView's width is match_parent , and in a horizontal LinearLayout, that would push the ImageView offscreen.

I suggest using weights, try:

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/ll"
    android:orientation="horizontal"
    android:background="@drawable/profile_background_head_item">

    <TextView
        android:id="@+id/txt"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/origin"
        android:gravity="center"
        android:textSize="20sp"
        android:textColor="#FFFFFFFF"
        />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_config"/>
    </LinearLayout>

This should have the TextView take up the rest of the horizontal space the ImageView leaves.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM