2011-04-16 82 views
1

我無法讓ImageButton顯示在屏幕的右側。如果我把ImageButton放在LinearLayout的內部,它會出現在左邊,但是當我把它放在內部的LinearLayout下時,它根本不會顯示。以下是xml文件。Android ImageButton不顯示在屏幕的右側

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

+0

設置重量。謝謝瑞恩 – avagadia 2011-04-16 17:13:31

0

您需要將ImageButton放入佈局中。這不是渲染,因爲你不告訴它在哪裏渲染。

您可以通過添加一個相對佈局來確定您想要的位置,方法之一是在那裏有一個相對佈局。使用相對佈局的圖像姿勢要正確http://developer.android.com/reference/android/widget/RelativeLayout.html

下面的文章具有內RelativeLayout的http://www.higherpass.com/Android/Tutorials/Exploring-Android-Linearlayout-And-Relativelayout/

+0

我已經把一個的ImageButton裏面的RelativeLayout,它仍不能正常呈現。我已更新帖子以在RelativeLayout中顯示ImageButton。 – avagadia 2011-04-16 16:40:42