Android XML:水平居中于底部

Android XML:水平居中于底部,android,xml,Android,Xml,正如您所看到的,在这段代码中,线性布局中有一个标题、两个线性布局和两个按钮。我要做的是把两个按钮放在中间。无论我做什么,我都无法使2个按钮在底部居中,高度为60像素 最后,我尝试使文本垂直和水平居中,并使底部的两个按钮水平居中。我需要改变什么 下面是布局编辑器中的图片。 在内部线性布局上,设置布局\u重力 这里有一个解决方案 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http

正如您所看到的,在这段代码中,线性布局中有一个标题、两个线性布局和两个按钮。我要做的是把两个按钮放在中间。无论我做什么,我都无法使2个按钮在底部居中,高度为60像素

最后,我尝试使文本垂直和水平居中,并使底部的两个按钮水平居中。我需要改变什么

下面是布局编辑器中的图片。

在内部线性布局上,设置
布局\u重力

这里有一个解决方案

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

<TextView 
    android:text="Title" 
    android:id="@+id/TextView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="30px" 
    android:textStyle="bold" 
    >
</TextView>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="60px">

<Button 
    android:text="Choose a Story" 
    android:id="@+id/choose" 
    android:layout_width="150px" 
    android:layout_height="wrap_content"  
    android:layout_gravity="bottom" 
    android:layout_marginBottom="1px">
</Button>

<Button 
    android:text="Info" 
    android:id="@+id/info" 
    android:layout_width="150px" 
    android:layout_height="wrap_content"  
    android:layout_gravity="bottom" 
    android:layout_marginBottom="1px">
</Button>

</LinearLayout>
</LinearLayout>

通常,您应该设置最外层组件(容器)的样式,然后该组件将相应地定位其所有子组件。另一方面,这种布局可以通过使用一个(或一对,具体取决于您要做什么)或
FrameLayout
s来实现,这将显著减少布局开销。虽然
线性布局没有什么问题,但它的计算代价惊人。

在内部线性布局上,设置
布局

这里有一个解决方案

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

<TextView 
    android:text="Title" 
    android:id="@+id/TextView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="30px" 
    android:textStyle="bold" 
    >
</TextView>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="60px">

<Button 
    android:text="Choose a Story" 
    android:id="@+id/choose" 
    android:layout_width="150px" 
    android:layout_height="wrap_content"  
    android:layout_gravity="bottom" 
    android:layout_marginBottom="1px">
</Button>

<Button 
    android:text="Info" 
    android:id="@+id/info" 
    android:layout_width="150px" 
    android:layout_height="wrap_content"  
    android:layout_gravity="bottom" 
    android:layout_marginBottom="1px">
</Button>

</LinearLayout>
</LinearLayout>


通常,您应该设置最外层组件(容器)的样式,然后该组件将相应地定位其所有子组件。另一方面,这种布局可以通过使用一个(或一对,具体取决于您要做什么)或
FrameLayout
s来实现,这将显著减少布局开销。虽然
LinearLayout
没有什么问题,但它的计算成本惊人。

不相关的旁注-使用原始像素值只是自找麻烦。相关问题-在“无论我做什么”中包含什么?将布局重力设置为“中心”?将布局重力设置为按钮上的线性布局水平居中并不能解决问题。主布局本身有多大?您的文本视图是否覆盖整个显示器的宽度?在布局编辑器中它是什么样子的?主布局只是填充父级。我的文本视图是包装内容。我将在一秒钟内拍摄这张照片。不相关的旁注-使用原始像素值只是自找麻烦。相关问题-在“无论我做什么”中包含了什么?将布局重力设置为“中心”?将布局重力设置为按钮上的线性布局水平居中并不能解决问题。主布局本身有多大?您的文本视图是否覆盖整个显示器的宽度?在布局编辑器中它是什么样子的?主布局只是填充父级。我的文本视图是包装内容。我马上给你拍张照片,你说这样行吗?出于某种原因,它看起来与我上面拍摄的照片非常相似。一个区别是“选择故事”按钮覆盖了大约60%的屏幕,信息按钮更小,垂直偏移约10px。我应该用你的脚本拍一张它最终是什么样子的照片吗?哎呀,我想我原来用外部
LinearLayout
的解决方案是行不通的。使用
FrameLayout
修复了它。另外,根据您使用布局的方式,您可以使用
merge
标记替换根
FrameLayout
,以降低布局的复杂性。您说这样行吗?出于某种原因,它看起来与我上面拍摄的照片非常相似。一个区别是“选择故事”按钮覆盖了大约60%的屏幕,信息按钮更小,垂直偏移约10px。我应该用你的脚本拍一张它最终是什么样子的照片吗?哎呀,我想我原来用外部
LinearLayout
的解决方案是行不通的。使用
FrameLayout
修复了它。此外,根据您使用布局的方式,您可以使用
merge
标记替换根
FrameLayout
,以降低布局复杂性。