ChatGPT解决这个技术问题 Extra ChatGPT

在线性布局中居中按钮

我正在使用线性布局来显示一个非常轻的初始屏幕。它有 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>
在具有水平对齐的 LinearLayout 中为我解决的问题是将 layout_width 设置为“wrap_content”。随后 layout_gravity 做了它应该做的事!
如果您严格需要使用 LinearLayout,我认为这应该可行:stackoverflow.com/questions/18051472/…

B
Bart Burg

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

与 RelativeLayout 相比,LinearLayout 更简单,重量更轻,应该更快一些。
我使用的是 layout_gravity 而不是重力。更改为 android:gravity="center" 为我解决了这个问题。菜鸟的错误。
@ackushiw 也是我的问题!
至少对我来说 android:gravity="center" LinearLayout 属性起到了作用。谢啦兄弟!
D
Dave Webb

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

请改用 RelativeLayout

所以替换:

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>

在某些情况下,您需要使用 LinearLayout 和居中内容。这不应该是公认的答案。如果可能的话,建议使用 RelativeLayout 会更好,如果不是,如何在 LinearLayout 中居中。
更不用说,RelativeLayout 的运行时间密集度更高,因为它们必须测量它们的尺寸,而 LinearLayout 超级简单且效率更高。
K
Kevin Crain

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


我喜欢这个选项,它可以让我保持 mi LinearLayout ! (不过,我不必在按钮中设置 android:weight="1" 以使其居中。)
android:gravity 在水平 LinearLayout 中对我不起作用,但 android:layout_gravity 可以。
h
hopcraft

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

android:layout_gravity="center"

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

android:layout_gravity="center_vertical|center_horizontal"

在父布局中添加 android:gravity="center" 属性 - 它包含您的按钮(子组件)。
您还需要 android.orientation="vertical" 加上父级中的 layout_gravity,也就是 LInearLayout 元素....
T
Tanmay Sahoo

添加这个

android:gravity="center"

在线性布局中。


应该是 android:gravity="center"
T
Taku

如果您使用 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>`

J
Javier

用这个很容易

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

T
Tim Cooper

您可以使用 RelativeLayout


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

上面的代码将起作用。


R
Reza Mamun

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

android:layout_gravity="center" 工作正常 :) 谢谢!
J
Joe Healy

我机器上的完整和工作样本...

A
Abubakar

您可以通过在父类中使用gravity="center" 属性来做到这一点,该属性使其子位置位于中心。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center">

M
Mohamed Adel

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

        android:layout_gravity="center" 

并使用

         android:layout_marginBottom="80dp"

         android:layout_marginTop="80dp"

改变位置


O
Oskar

在 Button 中设置为 true android:layout_alignParentTop="true" android:layout_centerHorizontal="true",如下所示:

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

https://i.stack.imgur.com/44D7i.jpg


u
user4058115

你也可以试试这个代码:

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

您能解释一下这与已经提供的答案有何不同吗?
A
A-Sharabiani

您还可以将 LinearLayout 的宽度设置为 wrap_content 并使用 android:layout_centerInParentandroid:layout_centerVertical="true"

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
>

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


M
Marci

这对我有用。

android:layout_alignParentEnd="true"

A
Abubakar

在这种情况下,您还可以使用 RelativeLayout 作为该 ImageButton 的父级

android:layout_centerInParent="true"

然后在 ImageButton 中使用上面的代码。


G
Gerard

你试过这个吗?

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

M
Mayank Mishra

利用

android:layout_centerHorizontal="true"


实际上 vogon101,这是一个相对布局中的有效属性。 :) 但是,它没有回答这个问题,因为用户使用的是 LinearLayout 并且想要垂直和水平居中。
u
user2090145

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

    android:layout_width="match_parent"

然后相应地给左右边距。

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

关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅