【问题标题】:RelativeLayout with TextView and ImageView带有 TextView 和 ImageView 的相对布局
【发布时间】:2018-01-10 08:14:52
【问题描述】:

我想为我的可扩展视图创建一个布局。 (在 EditText 内查看) 它由一个 ImageView 和一个 TextView 组成。

应该是这样的:

到目前为止我所拥有的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:padding="0dp">
    <ImageView
        android:id="@+id/closeImg"
        android:layout_width="28dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/closecross" />
    <TextView
        android:id="@+id/remarkText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="this text will not be shown :("
        android:layout_toLeftOf="@+id/closeImg"
        android:gravity="center" />
</RelativeLayout>

设计器正确显示它,但在运行时我看不到任何文本。我是在代码中分配文本还是直接在布局中分配文本

如何显示它,就像我在上图中显示的那样?

【问题讨论】:

  • 在您尝试访问上述布局的位置发布您的自定义视图
  • 我实际上做不到。我将 xamarin 用于 Visual Studio + C#。这是一个更大的班级。尽管如此,我也有一个包含一个文本视图的线性布局。如果我使用它,它会正确显示。
  • 使用权重.....
  • @Csharpest 看看我的回答,它将帮助您解决问题。

标签: android android-layout textview imageview android-relativelayout


【解决方案1】:

您可以在您的textview 中使用drawableRight

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="0dp">
<TextView
    android:id="@+id/remarkText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColor="#000000"
    android:padding="5dp"
    android:drawableRight="@drawable/closecross"
    android:drawableEnd="@drawable/closecross"
    android:text="this text will not be shown :("
    android:layout_toLeftOf="@+id/closeImg"
    android:gravity="center" />
</RelativeLayout>

现在您的文字与图像一起可见。 (我已经把我自己的图像替换为你的)

【讨论】:

  • 该死,这确实显示了图像。但奇怪的是,图像被切断了。只有约 60% 的宽度是可见的。有任何想法吗?这也可能来自我使用的可扩展构建器
  • 我已经修好了!!!非常感谢,剪掉是由于一些古老的代码......
  • @Csharpest 很高兴我能提供帮助。快乐编码:)
猜你喜欢
  • 1970-01-01
  • 2015-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多