Кнопка Android ImageButton не отображается в правой части экрана

#android #android-layout

#Android #android-макет

Вопрос:

Я не могу заставить ImageButton отображаться в правой части экрана. Если я помещу ImageButton над внутренним LinearLayout, он будет отображаться с левой стороны, но когда я помещу его под внутренним LinearLayout, он вообще не будет отображаться. Ниже приведен XML-файл.

 <?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>
  

Ответ №1:

Вот еще одно возможное решение. Вы можете установить вес внутреннего LinearLayout равным значению, например 1. Это позволит ImageButton отображать в пределах ограничений родительского элемента, а не снаружи.

 <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"
    ...
  

Для ссылки на документы:
http://developer.android.com/reference/android/widget/LinearLayout .LayoutParams.html#weight

Ответ №2:

Вам нужно поместить ImageButton внутри макета. Это не рендеринг, потому что вы не указываете ему, где отображать.

Один из способов, которым вы могли бы расположить его так, как вы хотите, — это добавить относительный макет после линейного, который у вас там есть. Используйте относительный макет, чтобы расположить изображение справа http://developer.android.com/reference/android/widget/RelativeLayout.html

В следующей статье содержится некоторая информация о позиционировании объектов в RelativeLayout http://www.higherpass.com/Android/Tutorials/Exploring-Android-Linearlayout-And-Relativelayout /

Комментарии:

1. Я поместил ImageButton внутри RelativeLayout, и он все еще не отображается. Я обновил сообщение, чтобы показать ImageButton внутри RelativeLayout.