2017-02-12 149 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,你就会明白