【问题标题】:Android: ImageView next to TextView not showingAndroid:TextView 旁边的 ImageView 未显示
【发布时间】:2017-02-12 02:26:49
【问题描述】:

我在ListView 行中有一个TextView 和ImageView,它们彼此相邻。但是,ImageView 根本没有出现,也没有记录点击。这是 XML 代码:

<?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 会显示,但在错误的位置。但我不明白为什么它会导致问题。我错过了什么?

【问题讨论】:

  • 尝试使用水平方向的线性布局
  • @Technicolor 似乎 android:layout_width="320dp" 对您的屏幕来说太多了,为什么您需要 320 dp?

标签: android xml android-relativelayout


【解决方案1】:

问题在于TextView 正在将ImageView 推离屏幕。

您可以使用LinearLayout 和android: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),那么它现在被声明为比其他两个更重要,因此它获得总剩余空间的一半,而前两个平分其余空间。

【讨论】:

    【解决方案2】:

    你需要使用带有权重的LinearLayout。如果你设置了固定宽度并且手机的尺寸很小,它要么会伸出屏幕。

    //做水平方向的线性布局

    <LinearLayout 
       ...
       orientation = "horizontal"
       ...
    >
    
        <TextView
            ....
            android:layout_width="0dp"
            android:layout_weight="1"
            ...
        />
    
        <Button
           ....
           android:layout_width="wrap_content"
           ...
        />
    
    </LinearLayout>
    

    玩android:layout_weight,你就明白了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 2014-09-06
      • 1970-01-01
      • 1970-01-01
      • 2014-07-29
      • 2016-02-20
      • 1970-01-01
      相关资源
      最近更新 更多