2017-06-04 56 views
1

我看到這樣的畫面這是一個意大利車牌模板: italian plate的TextView內的ImageView的

我的應用程序管理工具。我想用CardViewRecyclerView中顯示所有用戶的車輛,其中每個CardView包含一個ImageView(顯示前一圖像)和一個TextView,其中包含車牌。但我怎麼能把TextView放在正確的位置,與ImageView中顯示的圖像相比?而且,我認爲最糟糕的事情是,我怎麼能夠正確地爲所有屏幕分辨率放置TextView

+0

https://stackoverflow.com/questions/6931900/programmatically-set-left-drawable-in-a-textview –

+0

你也可以做一個困難的方式。在適當的佈局中添加imageview和textview併爲佈局賦予'stroke'背景。 – Pzy64

+0

@ cricket_007我不明白:如果我需要在這張圖片裏面寫**,我怎麼能用'drawableLeft'屬性來做這件事? – Clyky

回答

2

我已經嘗試了樣本代碼..

RES /佈局/ 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 /繪製/ 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

您可以將值更改爲你所希望的方式匹配.. 希望這有助於..

Bonus content!!(如果你想..)

+0

非常感謝你,但爲什麼兩個'LinearLayout's?第二秒很簡單,不是嗎? – Clyky

+0

是的。然後我才找到解決方案.. – Pzy64

0

跨越兩個視圖的最佳方法是使用FrameLayout作爲父級,而不是使用FrameLayout子級上的layout_gravity和margin屬性s來設置它們的位置

0

您可以將兩個視圖放在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>