ImageView 中的 TextView

3
我有这样一张图片,它是意大利车牌的模板:italian plate 我的应用程序管理车辆。我考虑使用RecyclerViewCardView显示所有用户的车辆,每个CardView包含一个ImageView(显示以前的图像)和一个TextView,其中包含车牌号码。但是如何将TextView放置在与ImageView中显示的图像相比的正确位置?并且,最糟糕的是,如何使TextView在所有屏幕分辨率下都能正确放置?

https://dev59.com/5Gw05IYBdhLWcg3w72TN - OneCricketeer
你也可以用更困难的方法来实现。在适当的布局中添加ImageView和TextView,并给布局添加“描边”背景。 - pz64_
@cricket_007 我不明白:如果我需要在这个图像里面写东西,我该如何使用 drawableLeft 属性来做到这一点? - user5892486
@Pzy64,你能给我一个例子吗? - user5892486
你似乎有两张图片。一张在左边,另一张围绕着盒子。 - OneCricketeer
@cricket_007 嗯,我明白了,我会尝试创建一个自定义的 TextView - user5892486
3个回答

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>

以下是它的外观。 enter image description here

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


非常感谢,但是为什么要用两个LinearLayout呢?只用第二个不就可以了吗? - user5892486

1
你可以将两个视图放在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>

0
最好的方法是使用FrameLayout作为父容器,然后在FrameLayout的子元素上使用layout_gravity和margin属性来设置它们的位置,以此来重叠两个视图。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接