简体   繁体   中英

ImageButton not showing to right of TextView in RelativeLayout

I need to have a ImageButton to right of TextView in RelativeLayout and I've tried this code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:background="#CC000000">

<TextView
    android:id="@+id/quality"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="20dp"
    android:layout_marginBottom="20dp"
    android:textColor="#666666"
    android:text="@string/default_bitrate" />


       <ImageButton
    android:id="@+id/arrow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null"
    android:layout_toRightOf="@+id/quality"
    android:src="@drawable/ico_arrow" >
</ImageButton>

The strange thing is that the ImageButton doesn't show with this scenario. I can see only the TextView. Where's the mistake ?

不要使TextView宽度与父级匹配,请要么用dp声明宽度,要么给textview和imagebutton权重,以便它们都可以占据屏幕上的空间

Change your xml with this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#CC000000">

    <TextView
        android:id="@+id/quality"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="20dp"
        android:layout_marginBottom="20dp"
        android:textColor="#666666"
        android:text="@string/default_bitrate" />


    <ImageButton
        android:id="@+id/arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/quality"
        android:src="@drawable/ico_arrow" >
    </ImageButton>
</RelativeLayout>

Change your textview width to wrap content. setting it to match_parent, textview will take the entire width of the screen hence you can't see the image

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#CC000000" >

    <TextView
        android:id="@+id/quality"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="20dp"
        android:text="@string/default_bitrate"
        android:textColor="#666666" />

     <ImageButton
        android:id="@+id/arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/quality"
        android:layout_toRightOf="@+id/quality"
        android:background="@null"
        android:src="@drawable/ico_arrow" >
    </ImageButton>

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