Android ImageView中的TextView

Android ImageView中的TextView,android,Android,我有一张意大利车牌的模板图片: 我的应用程序管理车辆。我曾想用CardView在RecyclerView中显示所有用户的车辆,其中每个CardView都包含一个ImageView(显示以前的图像)和一个TextView,其中包含车牌。但是与ImageView中显示的图像相比,如何将TextView置于正确的位置呢?而且,我认为最糟糕的事情是,我如何才能正确放置TextView,以适应所有屏幕分辨率 我尝试了一个示例代码 res/layout/numberplate.xml <?xml v

我有一张意大利车牌的模板图片:


我的应用程序管理车辆。我曾想用
CardView
RecyclerView
中显示所有用户的车辆,其中每个
CardView
都包含一个
ImageView
(显示以前的图像)和一个
TextView
,其中包含车牌。但是与
ImageView
中显示的图像相比,如何将
TextView
置于正确的位置呢?而且,我认为最糟糕的事情是,我如何才能正确放置
TextView
,以适应所有屏幕分辨率

我尝试了一个示例代码

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>
<?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>

res/drawable/background.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>
<?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>

以及它的样子。

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

重叠两个视图的最佳方法是使用FrameLayout作为父视图,而不是使用layout_gravity和margin属性​关于框架布局的子类​ 要设置它们的位置,可以将两个视图放置在相对位置并相互叠加。比如说,

<?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>


您也可以用更难的方式来完成。在一个合适的布局中添加imageview和textview,并为布局提供
笔划
背景。@cricket_007我不明白:如果我需要在这个图像中书写,我如何使用
drawableLeft
属性来实现这一点?@Pzy64你能给我举个例子吗?你似乎有两个图像。一个在左边,一个在盒子周围,非常感谢,但是为什么有两个
LinearLayout
s?第二个很简单,不是吗?