Android 在线性布局中居中按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1957831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:56:00  来源:igfitidea点击:

Center a button in a Linear layout

xmlandroidlayout

提问by themaninthesuitcase

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?

我正在使用线性布局来显示一个非常轻的初始屏幕。它有 1 个按钮,应该水平和垂直居中在屏幕上。但是,无论我尝试做什么,按钮都会顶部对齐中心。我在下面包含了 XML,有人能指出我正确的方向吗?

<?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>

回答by lancha90

Center using a LinearLayout:

使用 LinearLayout 居中:

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/btnFindMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/findme" />
</LinearLayout>

回答by Dave Webb

If you want to center an item in the middle of the screen don't use a LinearLayoutas these are meant for displaying a number of items in a row.

如果您想在屏幕中间居中显示一个项目,请不要使用 a,LinearLayout因为它们用于在一行中显示多个项目。

Use a RelativeLayoutinstead.

使用 aRelativeLayout代替。

So replace:

所以替换:

android:layout_gravity="center_vertical|center_horizontal"

for the relevant RelativeLayoutoption:

对于相关RelativeLayout选项:

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>

回答by rui

Have you tried defining android:gravity="center_vertical|center_horizontal"inside the layout and setting android:layout_weight="1"in the image?

您是否尝试过在图像中定义android:gravity="center_vertical|center_horizontal"布局和设置android:layout_weight="1"

回答by hopcraft

A commonly-used method that works with linear layout is to set a property on the image button

线性布局的一个常用方法是在图像按钮上设置一个属性

android:layout_gravity="center"

You can choose whether to left-align, center-align, or right-align each object in the linear layout. Note that the above line is precisely the same as

您可以选择是左对齐、居中对齐还是右对齐线性布局中的每个对象。请注意,上面的行与

android:layout_gravity="center_vertical|center_horizontal"

回答by Tanmay Sahoo

Add this

添加这个

android:gravity="center"

In LinearLayout.

在线性布局中。

回答by Hans Pour

If you use LinearLayoutyou can add gravity center:

如果你使用LinearLayout你可以添加重心:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center">
            <Button
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            />
</LinearLayout>`

回答by Javier

easy with this

用这个很容易

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:visibility="visible" 
        android:gravity="center"
        android:orientation="vertical" >

        <ProgressBar
            android:id="@+id/pbEndTrip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:text="Gettings" />
    </LinearLayout>

回答by Vish

<?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="match_parent" 
        android:layout_height="wrap_content"
        android:layout_gravity = "center"
        android:background="@drawable/findme">
    </ImageButton>

</LinearLayout>

The above code will work.

上面的代码会起作用。

回答by Amit kumar

You can use the RelativeLayout.

您可以使用RelativeLayout.

回答by Reza Mamun

As per the Android documentation for XML Attributes of android:layout_gravity, we can do it easily :)

根据android:layout_gravity 的XML 属性的 Android 文档,我们可以轻松完成:)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageButton android:id="@+id/btnFindMe" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"

        android:layout_gravity="center"

        android:background="@drawable/findme"></ImageButton>

</LinearLayout>