2017-02-12 76 views
0

我有一個TextViewImageViewListView行,彼此相鄰。但是,ImageView根本沒有顯示,也沒有註冊點擊。這是XML代碼:Android:TextView旁邊的ImageView沒有顯示

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/textView" 
     android:text="text" 
     android:layout_width="320dp" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:layout_centerVertical="true" 
     android:padding="10dp" /> 

    <ImageView 
     android:id="@+id/imageView" 
     android:clickable="true" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@id/textView" 
     android:src="@drawable/ic_action"/> 
</RelativeLayout> 

的問題似乎在於在layout_toRightOf行,如果我刪除它時,ImageView顯示,但在錯誤的地方。但我不明白爲什麼它會造成問題。我錯過了什麼?

+1

嘗試使用線性佈局與水平方向 – Deepesh

+0

@Technicolor似乎'的android:layout_width = 「320dp」'太很多爲你的屏幕爲什麼你完全需要320 DP? –

回答

1

問題是TextView正在將屏幕上的ImageView推下。

可以使用修復這個LinearLayoutandroid:layout_weight

如:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/textView" 
     android:text="text" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_marginTop="10dp" 
     android:layout_centerVertical="true" 
     android:padding="10dp" /> 

    <ImageView 
     android:id="@+id/imageView" 
     android:clickable="true" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@id/textView" 
     android:src="@drawable/ic_action"/> 
</LinearLayout> 

layout_weight屬性更多信息:

這個屬性的「重要性」值分配給在用詞上視圖它應該佔用多少空間在屏幕上。較大的重量值允許它擴大以填充父視圖中的任何剩餘空間。子視圖可以指定一個權重值,然後視圖組中的任何剩餘空間按其聲明權重的比例分配給子項。默認重量爲零。

例如,如果有三個文本字段,並且其中兩個文本字段聲明權重爲1,而另一個沒有權重,則沒有權重的第三個文本字段不會增長,只會佔用其內容所需的區域。在測量完所有三個場之後,另外兩個將平均擴展以填充剩餘的空間。如果第三個字段的權重爲2(而不是0),則現在聲明比其他兩個字段重要,因此它佔總剩餘空間的一半,而前兩個字段平分。

0

您需要使用帶重量的LinearLayout ..如果您設置固定寬度並且手機的尺寸很小,它將伸出屏幕。

//做的LinearLayout與水平方向

<LinearLayout 
    ... 
    orientation = "horizontal" 
    ... 
> 

    <TextView 
     .... 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     ... 
    /> 

    <Button 
     .... 
     android:layout_width="wrap_content" 
     ... 
    /> 

</LinearLayout> 

播放與android:layout_weight,你就會明白