Xml 在线性布局中居中放置按钮

Xml 在线性布局中居中放置按钮,xml,android,layout,Xml,Android,Layout,我使用线性布局来显示一个非常轻的初始屏幕。它有一个按钮,应该在屏幕的中心水平和垂直方向。然而,无论我尝试做什么,按钮将顶部对齐中心。我已经包含了下面的XML,有人能告诉我正确的方向吗 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"

我使用线性布局来显示一个非常轻的初始屏幕。它有一个按钮,应该在屏幕的中心水平和垂直方向。然而,无论我尝试做什么,按钮将顶部对齐中心。我已经包含了下面的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>


代码> < p>您是否已经尝试定义了<代码> Android:在布局和设置中的“Center、Stand、Center、Stand、Studio”、“代码”>“代码”> Android:LayOututhBug=“1”< /代码>在图像中?

< P>如果您想在屏幕中间设置一个项目,请不要使用<代码>线性布局< /代码>,因为这是为了显示一行中的多个项目。

改用a

因此,请替换:

android:layout_gravity="center_vertical|center_horizontal"
对于相关的
RelativeLayout
选项:

android:layout_centerInParent="true"
因此,布局文件将如下所示:

<?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>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
     <Button
        android:id="@+id/switch_flashlight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/turn_on_flashlight"
        android:textColor="@android:color/black"
        android:onClick="action_trn"
        android:background="@android:color/holo_green_light"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:padding="5dp" />
</RelativeLayout>

你试过这个吗

  <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainlayout" android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px"
android:background="#303030"
>
    <RelativeLayout
    android:id="@+id/widget42"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    >
    <ImageButton
    android:id="@+id/map"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="map"
    android:src="@drawable/outbox_pressed"
    android:background="@null"
    android:layout_toRightOf="@+id/location"
    />
    <ImageButton
    android:id="@+id/location"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="location"
    android:src="@drawable/inbox_pressed"
    android:background="@null"
    />
    </RelativeLayout>
    <ImageButton
    android:id="@+id/home"
    android:src="@drawable/button_back"
    android:background="@null"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5px"
android:orientation="horizontal"
android:background="#252525"
>
    <EditText
    android:id="@+id/searchfield"
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:background="@drawable/customshape"
    />
    <ImageButton
    android:id="@+id/search_button"
    android:src="@drawable/search_press"
    android:background="@null"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    />
</LinearLayout>
<com.google.android.maps.MapView
    android:id="@+id/mapview" 
    android:layout_weight="1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:clickable="true"
    android:apiKey="apikey" />

</TableLayout>

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

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

android:layout_gravity="center_vertical|center_horizontal"

使用线性布局居中:

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


您可以使用
相对布局
相对布局

使用相对布局会更容易,但对于线性布局,我通常通过确保宽度与父级匹配来居中:

    android:layout_width="match_parent"
然后给右边和左边相应的边距

    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
使用

android:layout\u centerHorizontal=“true”

别紧张

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

您也可以尝试以下代码:

<LinearLayout
            android:id="@+id/linear_layout"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:orientation="horizontal"
            android:weightSum="2.0"
            android:background="@drawable/anyBackground" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical"
            android:layout_weight="1" >

            <ImageView
                android:id="@+id/img_mail"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/yourImage" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical"
            android:layout_weight="1" >



<ImageView
            android:id="@+id/img_save"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:background="@drawable/yourImage" />
        </LinearLayout>
        </LinearLayout>

从我的机器上完成并运行样本


只需使用(使其位于布局的中心)

和使用

         android:layout_marginBottom="80dp"

         android:layout_marginTop="80dp"
要更改职位


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

上面的代码可以工作。

根据的Android XML属性文档,我们可以很容易地做到:)


添加此

android:gravity="center"

在LinearLayout中。

在按钮中设置为true
android:layout\u alignParentTop=“true”
android:layout\u centerHorizontal=“true”
,如下所示:

<?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>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
     <Button
        android:id="@+id/switch_flashlight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/turn_on_flashlight"
        android:textColor="@android:color/black"
        android:onClick="action_trn"
        android:background="@android:color/holo_green_light"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:padding="5dp" />
</RelativeLayout>


您还可以将线性布局的宽度设置为
包裹内容
,并使用
android:layout\u centerInParent
android:layout\u centerVertical=“true”



如果使用
线性布局
可以添加重心:

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

`

这对我很有用

android:layout_alignParentEnd="true"

我喜欢这个选项,它让我保持mi线性布局!(我不必在按钮中设置
android:weight=“1”
来居中。)android:gravity在水平线性布局中对我不起作用,但android:layou gravity对我起作用。在水平对齐的线性布局中,我的固定方法是将
layou width
设置为“包裹内容”。随后,
layout\u gravity
做了它应该做的事情!如果您严格地需要使用LinearLayout,我认为应该可以这样做:在父布局中添加安卓:gravity=“center”属性,该属性保存您的按钮(子组件)。您还需要安卓。orientation=“vertical”加上父布局中的布局\u gravity,又名LInearLayout元素……LInearLayout比RelativeLayout更简单、重量更轻,应该更快一些。我使用的是布局重力而不是重力。改为android:gravity=“center”为我解决了这个问题。新手失误。@ackushiw也是我的问题!至少对我来说,android:gravity=“center”LinearLayout属性起到了作用。谢谢你,兄弟!这应该是正确答案实际上是vogon101,这是RelativeLayout中的有效属性。:)但是,它没有回答这个问题,因为用户使用的是LinearLayout,并且希望垂直和水平居中。您能否解释一下这与已经提供的答案有什么不同?在某些情况下,您需要使用LinearLayout和center content。这不应该是公认的答案。如果可能的话,最好建议使用RelativeLayout,如果不是的话,建议如何在线性布局中居中。更不用说RelativeLayout由于测量维度的方式,运行时更加密集,然而,线性布局非常简单且效率更高。您只能在“RelativeLayout”的兄弟姐妹上使用“android:layout\u down”应该是
android:gravity=“center”
android:layout\u gravity=“center”工作正常:)谢谢!
android:layout_alignParentEnd="true"