2013-10-09 121 views
0

我有這樣的作爲佈局如何正確地設置該佈局

<RelativeLayout 
    android:layout_weight="1" 
    android:layout_height="match_parent" 
    android:id="@+id/home_btn" 
    style="@style/Home_Button"> 

    <TextView android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@color/white" 
     android:textStyle="bold" 
     android:gravity="center" 
     android:id="@+id/a" 
     android:text="@string/centres"/> 

    <ImageView android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:layout_above="@id/a" 
     android:src="@drawable/ic_home_btn"/> 
</RelativeLayout> 

我需要在RelativeLayout的爲中心的TextView的,並且ImageView的是TextView的上面的一部分,具有一組DP偏移(例如,圖片和文字之間的間距爲10dp)

我已嘗試過各種不同的方法和工作到目前爲止。我怎樣才能正確地得到這個工作?

哦,這裏的風格

<style name="Home_Button_NL"> 

    <item name="android:layout_height">wrap_content</item> 
    <item name="android:layout_width">match_parent</item> 
    <item name="android:background">@drawable/home_button</item> 
    <item name="android:textColor">@color/white</item> 
    <item name="android:gravity">center</item> 
    <item name="android:layout_marginTop">@dimen/padding_tiny</item> 
</style> 

回答

1

利用android:drawableLeft/Right/Top/Bottom的圖像定位到TextView的。此外,您還可以使用的TextView和繪製之間的一些填充有android:drawablePadding=""

使用方法如下:

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textColor="@color/white" 
    android:textStyle="bold" 
    android:gravity="center" 

    android:drawableTop="@drawable/ic_home_btn" 
    android:drawablePadding="10dp" 

    android:id="@+id/a" 
    android:text="@string/centres"/> 

這樣,你只需要在RelativeLayout的中心來定位一個視圖。

請注意,您無法以此方式縮放圖像。我建議這是因爲您的設置中沒有縮放比例。

0

使用android:center_horizontal = "true"alignParentBottom = "true"在你的TextView

0
<RelativeLayout 
    android:layout_weight="1" 
    android:layout_height="match_parent" 
    android:id="@+id/home_btn" 
    style="@style/Home_Button"> 

    <TextView android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@color/white" 
     android:textStyle="bold" 
     android:gravity="center" 
     android:id="@+id/a" 
     android:text="@string/centres" 
     android:centerInParent="true" // center the textview in it's parent 
    "/> 

    <ImageView android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:layout_above="@id/a" 
     android:src="@drawable/ic_home_btn" 
     android:layout_centerHorizontal="true" // center imageview horizontally 
     android:layout_marginTop="YOUR MARGIN HERE" 
    /> 
</RelativeLayout> 

這可以幫助!

+0

我其實已經嘗試過,我認爲它可以工作,但它將兩個元素居中,所以在圖像上方有相同的距離,因爲在文本下面有 –

+0

好的,請檢查我的編輯。 –

0

你試過:

android:paddingBottom="10dp"