首页 文章

将线性布局中的按钮居中

提问于
浏览
308

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

18 回答

  • 34

    如果您想将项目置于屏幕中间,请不要使用 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>
    
  • 2

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

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

  • 412

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

    android:layout_gravity="center"
    

    您可以选择是在线性布局中左对齐,居中对齐还是右对齐每个对象 . 注意上面的行与之完全相同

    android:layout_gravity="center_vertical|center_horizontal"
    
  • 5

    加上这个

    android:gravity="center"
    

    在LinearLayout中 .

  • 2

    这很容易

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

    你可以使用 RelativeLayout .

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

    上面的代码将起作用 .

  • -7

    来自我的机器的完整和可用的样品......

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:orientation="vertical"
        tools:context=".MainActivity"
        android:gravity="center"
        android:textAlignment="center">
    
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="My Apps!"
            android:id="@+id/textView"
            android:gravity="center"
            android:layout_marginBottom="20dp"
         />
    
        <Button
            android:layout_width="220dp"
            android:layout_height="wrap_content"
            android:text="SPOTIFY STREAMER"
            android:id="@+id/button_spotify"
            android:gravity="center"
            android:layout_below="@+id/textView"
            android:padding="20dp"
            />
    
        <Button
            android:layout_width="220dp"
            android:layout_height="wrap_content"
            android:text="SCORES"
            android:id="@+id/button_scores"
            android:gravity="center"
            android:layout_below="@+id/textView"
            android:padding="20dp"
            />
    
    
        <Button
            android:layout_width="220dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="LIBRARY APP"
            android:id="@+id/button_library"
            android:gravity="center"
            android:layout_below="@+id/textView"
            android:padding="20dp"
            />
    
        <Button
            android:layout_width="220dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="BUILD IT BIGGER"
            android:id="@+id/button_buildit"
            android:gravity="center"
            android:layout_below="@+id/textView"
            android:padding="20dp"
            />
    
        <Button
            android:layout_width="220dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="BACON READER"
            android:id="@+id/button_bacon"
            android:gravity="center"
            android:layout_below="@+id/textView"
            android:padding="20dp"
            />
    
        <Button
            android:layout_width="220dp"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="CAPSTONE: MY OWN APP"
            android:id="@+id/button_capstone"
            android:gravity="center"
            android:layout_below="@+id/textView"
            android:padding="20dp"
            />
    
    </LinearLayout>
    
  • 0

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

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

    您也可以尝试此代码:

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

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

    android:layout_gravity="center"
    

    并使用

    android:layout_marginBottom="80dp"
    
             android:layout_marginTop="80dp"
    

    改变位置

  • 0

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

    enter image description here

  • 0

    您还可以将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"/>
    
  • 8

    你试过这个吗?

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

    使用

    android:layout_centerHorizontal =“true”

  • -2

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

    android:layout_width="match_parent"
    

    然后相应地给出左右边距 .

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

相关问题