我正在使用线性布局来显示一个非常轻的初始屏幕。它有 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>
layout_width
设置为“wrap_content”。随后 layout_gravity
做了它应该做的事!
使用 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>
如果您想在屏幕中间居中显示项目,请不要使用 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>
您是否尝试过在布局中定义 android:gravity="center_vertical|center_horizontal"
并在图像中设置 android:layout_weight="1"
?
android:weight="1"
以使其居中。)
与线性布局一起使用的常用方法是在图像按钮上设置属性
android:layout_gravity="center"
您可以选择是左对齐、居中对齐还是右对齐线性布局中的每个对象。请注意,上面的行与
android:layout_gravity="center_vertical|center_horizontal"
添加这个
android:gravity="center"
在线性布局中。
android: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>`
用这个很容易
<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>
您可以使用 RelativeLayout
。
<?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: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>
我机器上的完整和工作样本...
您可以通过在父类中使用gravity="center" 属性来做到这一点,该属性使其子位置位于中心。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center">
只需使用 ( 使其位于布局的中心)
android:layout_gravity="center"
并使用
android:layout_marginBottom="80dp"
android:layout_marginTop="80dp"
改变位置
在 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
你也可以试试这个代码:
<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>
您还可以将 LinearLayout 的宽度设置为 wrap_content
并使用 android:layout_centerInParent
、android: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"/>
这对我有用。
android:layout_alignParentEnd="true"
在这种情况下,您还可以使用 RelativeLayout 作为该 ImageButton 的父级
android:layout_centerInParent="true"
然后在 ImageButton 中使用上面的代码。
你试过这个吗?
<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_centerHorizontal="true"
使用相对布局会更容易,但对于线性布局,我通常通过确保宽度与 parent 匹配来居中:
android:layout_width="match_parent"
然后相应地给左右边距。
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"