开发者

Android ImageButton not displaying on right side of the screen

I can't get the ImageButton to show up on the right side of the screen. If I place the ImageButton above the inner LinearLayout, it will show up on the left side, but when I place it below the inner LinearLayout, it doesn't display at all. Below is the xml file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="horizontal" android:padding="2dip"
    style="@style/default_style">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/layout1"
        android:orientation="vertical" >    

        <TableLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:id="@+id/headerTable"
            android:stretchColumns="0,1">
            <TableRow android:layout_width="fill_parent" android:id="@+id/headerRow"
                android:layout_height="match_parent">
                <TextView android:text="Fullname" android:id="@+id/fullname"
                    android:layout_height="wrap_content" android:layout_gravity="left"
                    android:layout_width="fill_parent" android:textStyle="bold"
                    style="@style/default_style"></TextView>
                <TextView android:text="ContentTimestamp" android:id="@+id/content_timestamp"
                    android:layout_height="wrap_content" android:layout_gravity="right"
                    android:layout_width="fill_parent"
                    style="@style/default_style"></TextView>
                <ImageView android:id="@+id/hasGeoLocation"
                    android:layout_width="wrap_content" android:layout_height="wrap_content"
                    android:src="@drawable/map" android:paddingLeft="2dip" android:visibility="visible">
                </ImageView>    
            </TableRow>

        </TableLayout>

        <TextView android:id="@+id/content"
            android:text="Content blah blah blah blah blah blah blah blah blah"
         开发者_运维问答   android:layout_width="fill_parent" android:gravity="center_vertical"
            android:textSize="16dip" android:layout_height="wrap_content"
            android:paddingLeft="5dip"
            style="@style/default_style"> </TextView>

    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >                      
        <ImageButton android:id="@+id/hasAudio"
            android:layout_width="50px"
            android:layout_height="50px"
            android:layout_toRightOf="@+id/layout1"
            android:gravity="right"
            android:src="@drawable/play"
            android:visibility="visible" />     
    </RelativeLayout>
</LinearLayout>


Here's another possible solution. You can set the weight of the inner LinearLayout to a value, such as 1. That will allow the ImageButton to render within the constraints of the parent rather than outside.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="horizontal" android:padding="2dip"
    style="@style/default_style">
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_vertical" 
        android:layout_weight="1">
        <TableLayout android:layout_width="fill_parent"
    ...

For reference to the docs: http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html#weight


You need to put the ImageButton inside a layout. It's not rendering because you're not telling it where to render.

One way you could position it how you want to is by adding a relative layout after the Linear one you have there. Use a relative layout to position the image to the right http://developer.android.com/reference/android/widget/RelativeLayout.html

The following article has some information about positioning things within a RelativeLayout http://www.higherpass.com/Android/Tutorials/Exploring-Android-Linearlayout-And-Relativelayout/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜