在一个线性布局中心按钮 [英] Centre a button in a Linear layout
问题描述
我使用的是线性布局以显示pretty的光初始画面。它有1个按钮应该是在屏幕水平和垂直居中的。但无论怎样我尝试做的按钮,将顶部居中。我已经包含了XML的下方,可以有人点我在正确的方向?
感谢
< XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< ImageButton的机器人:ID =@ + ID / btnFindMe
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =center_vertical | center_horizontal
机器人:背景=@可绘制/ findme>< / ImageButton的>
< / LinearLayout中>
如果要居中在屏幕中间不使用的LinearLayout
作为项目这些都是为了在一排展示了一些项目。
使用一个<一个href="http://developer.android.com/reference/android/widget/RelativeLayout.html"><$c$c>RelativeLayout$c$c>代替。
所以替换:
安卓layout_gravity =center_vertical | center_horizontal
有关相关的 RelativeLayout的
选项:
安卓layout_centerInParent =真
所以,你的布局文件看起来是这样的:
&LT; XML版本=1.0编码=UTF-8&GT?;
&LT; RelativeLayout的机器人:ID =@ + ID / RelativeLayout01
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
的xmlns:机器人=http://schemas.android.com/apk/res/android&GT;
&LT; ImageButton的机器人:ID =@ + ID / btnFindMe
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerInParent =真
机器人:背景=@可绘制/ findme&GT;&LT; / ImageButton的&GT;
&LT; / RelativeLayout的&GT;
I am using a linear layout to display a pretty light initial screen. It has 1 button that is supposed to centre in the screen both horizontally and vertically. However no matter what I try to do the button will top align centre. I have included the XML below, can some one point me in the right direction?
Thanks
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton android:id="@+id/btnFindMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:background="@drawable/findme"></ImageButton>
</LinearLayout>
If you want to center an item in the middle of the screen don't use a LinearLayout
as these are meant for displaying a number of items in a row.
Use a RelativeLayout
instead.
So replace:
android:layout_gravity="center_vertical|center_horizontal"
for the relevant RelativeLayout
option:
android:layout_centerInParent="true"
So your layout file will look like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton android:id="@+id/btnFindMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/findme"></ImageButton>
</RelativeLayout>
这篇关于在一个线性布局中心按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!