我使用的是线性布局以显示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
作为项目这些都是为了在一排展示了一些项目。
![线性布局](/d/file/2023/09-17/7ff804084ce7800a75fad613510c7f9d.png)
使用一个RelativeLayout代替。
所以替换:
安卓layout_gravity =center_vertical | center_horizontal
有关相关的 RelativeLayout的
选项:
安卓layout_centerInParent =真
所以,你的布局文件看起来是这样的:
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的机器人:ID =@ + ID / RelativeLayout01
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
的xmlns:机器人=http://schemas.android.com/apk/res/android>
< ImageButton的机器人:ID =@ + ID / btnFindMe
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerInParent =真
机器人:背景=@可绘制/ findme>< / ImageButton的>
< / RelativeLayout的>
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>
相关推荐
最新文章