在一个线性布局中心按钮线性、布局、按钮、中心

由网友(心却寂寥゜)分享简介:我使用的是线性布局以显示pretty的光初始画面。它有1个按钮应该是在屏幕水平和垂直居中的。但无论怎样我尝试做的按钮,将顶部居中。我已经包含了XML的下方,可以有人点我在正确的方向?感谢< XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=htt​​p://s...

我使用的是线性布局以显示pretty的光初始画面。它有1个按钮应该是在屏幕水平和垂直居中的。但无论怎样我尝试做的按钮,将顶部居中。我已经包含了XML的下方,可以有人点我在正确的方向?

感谢

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://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_horizo​​ntal
    机器人:背景=@可绘制/ findme>< / ImageButton的>

< / LinearLayout中>
 

解决方案

如果要居中在屏幕中间不使用的LinearLayout 作为项目这些都是为了在一排展示了一些项目。

线性布局

使用一个RelativeLayout代替。

所以替换:

 安卓layout_gravity =center_vertical | center_horizo​​ntal
 

有关相关的 RelativeLayout的选项:

 安卓layout_centerInParent =真
 

所以,你的布局文件看起来是这样的:

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的机器人:ID =@ + ID / RelativeLayout01
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    的xmlns:机器人=htt​​p://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>

阅读全文

相关推荐

最新文章