【问题标题】:TextView inside an ImageViewImageView 中的 TextView
【发布时间】:2017-06-04 14:39:26
【问题描述】:

我有这张图片,它是意大利车牌的模板:

我的应用管理车辆。我想在RecyclerViewCardView 中显示所有用户的车辆,其中每个CardView 包含一个ImageView(显示上一张图像)和一个TextView,其中包含车牌。但是,与ImageView 中显示的图像相比,我该如何将TextView 放在正确的位置?而且,我认为最糟糕的事情是,我该如何为所有屏幕分辨率正确放置TextView

【问题讨论】:

  • 你也可以用更难的方式来做。在合适的布局中添加 imageview 和 textview,并为布局提供 stroke 背景。
  • @cricket_007 我不明白:如果我需要inside这个图片,我怎么能使用drawableLeft属性来做到这一点?
  • @Pzy64 能给我举个例子吗?
  • 您似乎有两个图像。一个在左边,一个在框的周围

标签: android


【解决方案1】:

我已经尝试了一个示例代码..

res/layout/numberplate.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/background"
    android:orientation="horizontal">

<LinearLayout
    android:layout_margin="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <ImageView
        android:layout_width="46dp"
        android:layout_height="46dp"
        android:background="@mipmap/ic_launcher" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:layout_marginLeft="20dp"
        android:gravity="center"
        android:text="Number"
        android:textSize="25dp" />
    </LinearLayout>
</LinearLayout>
</LinearLayout>

res/drawable/background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#d0d0d0" />
<corners android:radius="5dp" />
<stroke android:color="#202020" android:width="3dp"/>
</shape>

以及它的外观。

您可以更改值以匹配您想要的方式.. 希望这会有所帮助..

【讨论】:

  • 非常感谢,但为什么要两个LinearLayouts?只需一秒钟就很简单,不是吗?
【解决方案2】:

您可以将两个视图放在一个 RelativeLayout 中并相互叠加。例如,

<?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="wrap_content"
     android:padding="30dp">
     <ImageView
         android:layout_width="46dp"
         android:layout_height="46dp"
         android:layout_gravity="center"
         android:background="@drawable/your_numberplate_image" />
     <TextView
         android:layout_width="46dp"
         android:layout_height="46dp"
         android:layout_marginLeft="10dp"
         android:gravity="center"
         android:text="Number"
         android:textSize="25dp" />
</RelativeLayout>

【讨论】:

    【解决方案3】:

    重叠两个视图的最佳方法是使用 FrameLayout 作为父级,而不是在 FrameLayout 子级上使用 layout_gravity 和 margin 属性来设置它们的位置

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多