2014-12-21 41 views
0

我想中心的所有內容佈局,以中心但這是我有問題:中心的ImageView和TextView的並排

enter image description here

正如你所看到的,我想中心的圖像和文字在他們的父母但想要將圖像留在文字左側。

這裏是佈局代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/app_light_green" > 

    <ImageView 
     android:id="@+id/toastImage" 
     android:layout_width="24dp" 
     android:layout_height="24dp" 
     android:layout_centerInParent="true" 
     android:paddingTop="1dp" 
     android:src="@drawable/exclamation" /> 

    <TextView 
     android:id="@+id/toastText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:gravity="center" 
     android:paddingBottom="3dp" 
     android:paddingLeft="5dp" 
     android:paddingRight="3dp" 
     android:paddingTop="2dp" 
     android:textColor="@color/app_darker_green" 
     android:textSize="@dimen/sixteen_sp" /> 

</RelativeLayout> 
+0

看看https://stackoverflow.com/a/39958875/3278589 – Subho

回答

4

您可以使用水平的LinearLayout,往其中加你的TextView和ImageView的,然後在RelativeLayout的添加的LinearLayout並使其中心。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/RelativeLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:orientation="horizontal" > 

    <ImageView 
     android:id="@+id/toastImage" 
     android:layout_width="24dp" 
     android:src="@drawable/exclamation" 
     android:layout_height="24dp" 
     android:paddingTop="1dp" /> 

    <TextView 
     android:id="@+id/toastText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:paddingBottom="3dp" 
     android:paddingLeft="5dp" 
     android:paddingRight="3dp" 
     android:paddingTop="2dp" 
     android:textColor="@color/app_darker_green" 
     android:textSize="@dimen/sixteen_sp"/> 
</LinearLayout> 

</RelativeLayout> 
+0

可以修改我的代碼請? – Dev01

+0

它是否按照你的想法工作? – strings95

+0

感謝它工作完美:) – Dev01

1

嘗試使用framelayout。 在framelayout中,添加imageview和textview,爲imageview指定重力,爲textview指定重心。爲了使它與textview保持一致,最後給imageview留下一些左邊距。

與RelativeLayout的備用SOLN是,在您的ImageView的標籤使用屬性 align_toLeftOf =「@ + ID/R.id.Textview1"

而從imageview的父除去中心。 您的問題將得到解決。

請忽略我已經輸入從手機這個答案的語法。:)

0

我也帶來minel..just爲簡單起見

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/RelativeLayout01" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@color/app_light_green" > 


<TextView 
    android:id="@+id/toastText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:gravity="center" 
    android:paddingBottom="3dp" 
    android:paddingLeft="5dp" 
    android:drawableLeft="@drawable/exclamation" 
    android:paddingRight="3dp" 
    android:paddingTop="2dp" 
    android:textColor="@color/app_darker_green" 
    android:textSize="@dimen/sixteen_sp" /> 

</RelativeLayout> 

跟我不必擔心imageview的,你只是定位您的TextView在這一切的中心..

+0

你正在把填充和使用drawableLeft這不是解決方案 – Subho

相關問題