java - Android 布局中的绝对居中

标签 java android xml android-layout

我花了很多时间试图解决这个问题。我正在构建一个对话框布局,并尝试使标题相对于对话框居中,同时标题两侧各有 2 个按钮。这些按钮将被“重力”地推向最左边和最右边。

最简单的部分是将对话框居中,或者执行最左边和最右边的按钮。这让他们在同一条线上相处融洽。

简单的思考方式:

  1. 标题位于对话框中央
  2. 2 个按钮在同一行上独立左右对齐

关于如何做到这一点有什么想法吗?

最佳答案

IMO 我会尝试做一个自定义对话框,并使用 TableLayout 来放置您的元素。 这样你就可以放置按钮,并使用标题上的属性stretchcolumn来按下屏幕一侧的按钮。 (如果我能很好地理解你的话)
alt text http://img801.imageshack.us/img801/4372/tablelayout.png
如果你使用这个 xml 示例,它应该可以工作并且独立于屏幕尺寸/旋转:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:stretchColumns="1">

<TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <Button android:text="Button01" android:id="@+id/Button01"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
    <TextView android:text="This is your title" android:id="@+id/TextView01"
        android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" ></TextView>
    <Button android:text="Button02" android:id="@+id/Button02"
        android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</TableRow>
</TableLayout>

关于java - Android 布局中的绝对居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3313579/

相关文章:

java - 搜索从 xml 文档创建的 DOM 并将值复制到电子表格/csv

c# - Xml 序列化为 XmlAttribute : namespace prefix lost when Element in same namespace

java - 数一下没有。使用给定的输入字符串可以创建单词的次数

android - BadParcelableException :ClassNotFoundException when unmarshalling: android. support.v4.app.FragmentManagerState

android - android中按钮的下拉菜单

java - 从 POJO 生成 Map<String,String>

java - 在 styles.xml 文件中为所有按钮声明一个通用的 onClick 属性

Java 标记字符串

java - 在 2 台 PC 之间创建信任证书

Java Collections Sort 不接受带有 arg 的比较器构造函数