简体   繁体   中英

ImageButton in Layout always invisible

I'm trying to make a layout for list items with an image to the left, two lines of text and the three little squares on the right, for a popup menu.

I have tried with LinearLayout and RelativeLayout , but the ImageButton element is always invisible. The preview shows as if the width was 0. I have no idea what to do, even setting layout_width and minWidth to some value did nothing.

Here is the XML for the element:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="right"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">

    <ImageView
        android:contentDescription="@string/content_description_collection_logo"
        android:id="@android:id/icon"
        android:layout_marginTop="8dip"
        android:src="@drawable/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:id="@+id/text_layout"
        android:layout_toRightOf="@android:id/icon"
        android:layout_marginTop="8dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@android:id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@android:id/text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/text1"
            android:layout_alignStart="@android:id/text1"
            android:layout_marginTop="5dip"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </RelativeLayout>

    <ImageButton
        android:id="@+id/popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/text_layout"
        android:src="@drawable/ic_action_overflow"
        android:contentDescription="@string/content_description_overflow_button"
        android:layout_alignBottom="@android:id/icon"
        android:layout_above="@+id/text_layout"
        android:minWidth="1dp" />

</RelativeLayout>

I read that setting minWidth to 1dp can help here, but it unfortunately did not. The Drawable exists and is 32x32 pixel png.

There are a couple of things wrong with that layout:

  • android:orientation="horizontal" isn't relevant for a RelativeLayout
  • android:gravity="right" is probably not what you want for the list entry, should probably left aligned?
  • Add android:layout_alignParentLeft="true" to the ImageView to anchor it to the left side of the layout
  • android:layout_above="@+id/text_layout" in the ImageButton doesn't make sense if it should be to the right of the two TextViews.

For your case it makes sense to use two nested LinearLayouts or one RelativeLayout. RelativeLayouts are sometimes good to replace nested LinearLayouts but I use them only of the number of ViewGroups can be reduced since it can be tricky to "configure" them correctly (alignment, filling the whole width...).

In your case I'd go for the LinearLayout approach because it's hard to position the ImageButton to the right of the two TextViews without adding an extra container view (as you did):

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="left|center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">

    <ImageView
        android:contentDescription="@string/content_description_collection_logo"
        android:id="@android:id/icon"
        android:layout_marginTop="8dip"
        android:src="@drawable/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_marginTop="8dip"
        android:layout_weight="1"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@android:id/text1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@android:id/text2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/text1"
            android:layout_marginTop="5dip"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </LinearLayout>

    <ImageButton
        android:id="@+id/popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_overflow"
        android:contentDescription="@string/content_description_overflow_button"
        android:minWidth="1dp" />

</LinearLayout>

Try to change the attr android:layout_above to android:layout_below on yout ImageButton:

<ImageButton
    android:id="@+id/popup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/text_layout"
    android:src="@drawable/ic_action_overflow"
    android:contentDescription="@string/content_description_overflow_button"
    android:layout_alignBottom="@android:id/icon"
    **android:layout_below="@+id/text_layout"**
    android:minWidth="1dp" />

I think thats a RelativeLayout problem, cause your text_layout is aligned at the top of the main view, and when you put layout_above your ImageButton will be placed above of the text_layout, in other words, will be placed off the screen

Try this way,hope this will help you to solve your problem.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
    android:padding="5dp">

    <ImageView
        android:contentDescription="@string/content_description_collection_logo"
        android:id="@android:id/icon"
        android:src="@drawable/ic_launcher"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:id="@+id/text_layout"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_marginLeft="5dp"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@android:id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@android:id/text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </LinearLayout>

    <ImageButton
        android:id="@+id/popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:src="@drawable/ic_action_overflow"
        android:contentDescription="@string/content_description_overflow_button"
        android:minWidth="1dp" />

</LinearLayout>

RelativeLayout Solution

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd">

    <ImageView
        android:id="@android:id/icon"
        android:layout_marginTop="8dip"
        android:src="@drawable/ic_launcher"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ImageButton
        android:id="@+id/popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_overflow"
        android:contentDescription="@string/content_description_overflow_button"        
        android:layout_alignParentRight="true"
        android:layout_alignBottom="@android:id/icon" />

    <RelativeLayout
        android:id="@+id/text_layout"
        android:layout_toRightOf="@android:id/icon"
        android:layout_toLeftOf="@+id/popup"
        android:layout_marginTop="8dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@android:id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@android:id/text2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/text1"
            android:layout_alignStart="@android:id/text1"
            android:layout_marginTop="5dip"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </RelativeLayout>

</RelativeLayout>

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