0

我有一個內部RelativeLayout的RelativeLayout。它包含左側的TextView和右側的ImageButton(星號)。ImageButton重疊問題

The whole layout

正如你可以看到ImageButton的overlapps文字雖然我設置的ImageButton的標籤layout_alignEnd和layout_alignRight。

這是我內心的RelativeLayout:

<RelativeLayout 
    android:id="@+id/layout_question" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/toolbar" 
    android:layout_toEndOf="@+id/tv_topic" 
    android:layout_toRightOf="@+id/tv_topic" 
    android:gravity="top" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/tv_question" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:padding="5dp" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textStyle="bold"/> 

    <ImageButton 
     android:id="@+id/bt_favorite" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignEnd="@+id/tv_question" 
     android:layout_alignRight="@+id/tv_question" 
     android:background="#00ffffff" 
     android:src="@drawable/star"/> 
</RelativeLayout> 

我想達到的文本總是在ImageButton的左側,而且從來沒有的ImageButton的overlapps文本。

這裏有什麼問題?

+0

使用'android:layout_alignBottom =「@ + id/tv_question」'in imagebutton – knownUnknown

回答

1

只需使用layout_toLeftOf財產TextView

android:layout_toLeftOf="@+id/bt_favorite" 

你可以根據使用layout_alignParentRight財產ImageView

android:layout_alignParentRight="true" 

剛剛嘗試這一點

<RelativeLayout 
    android:id="@+id/layout_question" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/toolbar" 
    android:layout_toEndOf="@+id/tv_topic" 
    android:layout_toRightOf="@+id/tv_topic" 
    android:gravity="top" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/tv_question" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_toLeftOf="@+id/bt_favorite" 
     android:padding="5dp" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textStyle="bold"/> 

    <ImageButton 
     android:id="@+id/bt_favorite" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:background="#00ffffff" 
     android:src="@drawable/star"/> 
</RelativeLayout> 
+0

謝謝。這解決了這個問題 –

1

使相對線性的,調整權重你的要求換貨。

<LinearLayout 
     android:id="@+id/layout_question" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="top" 
     android:orientation="horizontal" 
     android:weightSum="6"> 

     <TextView 
      android:id="@+id/tv_question" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:padding="5dp" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:textStyle="bold" 
      android:layout_weight="5"/> 

     <ImageButton 
      android:id="@+id/bt_favorite" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_alignEnd="@+id/tv_question" 
      android:layout_alignRight="@+id/tv_question" 
      android:background="#00ffffff" 
      android:src="@mipmap/ic_launcher" 
      android:layout_weight="1"/> 
    </LinearLayout> 
1

基本上,你不需要設置您的TextView(tv_question)到 android:layout_alignParentLeft="true" 導致其寬度爲match_parent。所以如果你想讓你的textview只是在你的imageview旁邊,那就這樣做。 首先解決您的ImageView的位置,讓我們說,右對齊,然後,讓TextView的僅僅是imageview的

<ImageButton 
    android:id="@+id/bt_favorite" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:background="#00ffffff" 
    android:src="@drawable/star"/> 
<TextView 
    android:id="@+id/tv_question" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_toLeftOf="@+id/bt_favorite" 
    android:padding="5dp" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:textStyle="bold"/> 

終於離開身邊,你沒有設置你的RelativeLayout ATTR - 安卓方向,它不起作用。 試試這個,它應該工作。