我将此作为布局的一部分
<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>
我需要TextView在RelativeLayout中居中,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>
答案 0 :(得分:1)
利用android:drawableLeft/Right/Top/Bottom
将图像定位到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的中心。
请注意,您无法通过这种方式缩放图片。我建议这样做,因为你的设置没有缩放。
答案 1 :(得分:0)
在TextView中使用android:center_horizontal = "true"
和alignParentBottom = "true"
答案 2 :(得分: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>
这可能有所帮助!
答案 3 :(得分:0)
你试过了吗?
android:paddingBottom="10dp"