Выровнять кнопки внутри TableRow
Я разрабатываю приложение для Android.
У меня есть это:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="200px"
android:orientation="vertical">
<TextView
android:id="@+id/gameTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="left"
android:layout_margin="5px"
android:text="aaaaa"/>
<TextView
android:id="@+id/gameDescription"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="left"
android:layout_margin="5px"
android:typeface="sans"
android:textSize="16sp"
android:textStyle="bold"
android:text="dddddd"/>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ButtonsTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="@string/yes"
android:id="@+id/playGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="@string/no"
android:id="@+id/noPlayGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
</LinearLayout>
Я хочу поставить кнопку playGame в левой части центра и кнопку noPlayGame в правой части центра.
Теперь оба выровнены слева от TableRow.
Как я могу это сделать?
Спасибо.
EDIT. Я добавил полный макет и изменения, предложенные Ashay.
2nd EDIT:
РЕШЕНИЕ
Я добавил в TableLayout
следующее: android:stretchColumns="0,1"
. Теперь кнопки выравниваются по центру, но они заполняют колонку энтузиастов.
Ответы
Ответ 1
Надеюсь, это поможет:
<TableRow
android:id="@+id/tableRow1"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="@string/stringtxt1"></Button>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:text="@string/stringtxt2"
android:layout_height="wrap_content"
android:layout_gravity="center|center_horizontal"></Button>
</TableRow>
Ответ 2
Я разместил свой код, надеюсь, это поможет другим пользователям решить свои проблемы (в центре контента). Я хотел бы центрировать две кнопки в строке таблицы, вот мой код, который решает эту проблему:
мы укажем, какие столбцы будут растянуты в prop android: strechColumns = "a, b..", используя список с разделителями-запятыми
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/buttonGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1" >
<TableRow>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button1Name"
android:layout_gravity="right"/>
<Button
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button2Name"
android:layout_gravity="left"/>
</TableRow>
</TableLayout>
Я надеюсь, что это поможет. Извините за мой английский:/
Ответ 3
Для центровки одной кнопки в одной строке:
<TableRow android:id="@+id/rowNameHere"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:gravity="center">
<Button android:id="@+id/btnDoStuff"
android:text="Do Stuff" />
</TableRow>
После рассмотрения вышеуказанных ответов я схватил несколько вещей из каждого ответа, и это сработало для меня.
Ответ 4
Удалите эту строку <TableRow android:layout_gravity="center">
И дайте android:fill_containt
в строке таблицы для height и width
ОТДЕЛЬНЫЙ ОТВЕТ:
Ну, мне очень жаль последних сообщений о правописании.
Через XML это невозможно для TableLayout
. Вы должны установить этот пользовательский интерфейс в код в oncreate.
В вашем java файле вы можете установить их, используя ниже.
playGame.setWidth(ScreenWidth()/2-var);
noPlayGame.setWidth(ScreenWidth()/2-var);
здесь var
можно использовать для установки расстояния между этими двумя кнопками, такими как 10 или 30.
Кроме того, ваш xml получит изменение следующим образом:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ButtonsTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TableRow
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center">
<Button
android:text="yes"
android:id="@+id/playGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
<Button
android:text="no"
android:id="@+id/noPlayGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
</TableRow>
</TableLayout>
Метод ScreenWidth()
можно использовать для получения ширины экрана с помощью этих методов.
private int ScreenWidth() {
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
String width=""+dm.widthPixels;
return Integer.parseInt(width);
}
Ответ 5
Я добавил два фиктивных текста для выравнивания моей кнопки в центре.
<TableRow>
<TextView
android:text=""
android:layout_weight=".25"
android:layout_width="0dip"
/>
<Button
android:id="@+id/buttonSignIn"
android:text="Log In"
android:layout_height="wrap_content"
android:layout_weight=".50"
android:layout_width="0dip"
/>
<TextView
android:text=""
android:layout_weight=".25"
android:layout_width="0dip"
/>
</TableRow>