2014-12-21 43 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

相关问题