Совместимость Android Layout Right Align
У меня есть следующий макет на месте
<?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">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_weight="1">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="13">
<LinearLayout android:id="@+id/LinearLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content">
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">
<ImageButton android:background="@null" android:id="@+id/back" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/back" android:padding="10dip" />
</LinearLayout>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1">
<ImageButton android:background="@null" android:id="@+id/forward" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/forward" android:padding="10dip" />
</LinearLayout>
</LinearLayout>
<RelativeLayout android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" >
<ImageButton android:background="@null" android:id="@+id/special" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="@drawable/barcode" android:padding="10dip" android:layout_gravity="right"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Для этого вопроса меня беспокоит только нижняя половина макета. Сейчас он содержит 3 кнопки изображения. Первые 2, я хочу, чтобы рядом друг с другом выровнялись влево. Третий, я хочу быть выровнен с правой стороны.
Как есть, первые 2 кнопки - это то место, где я хочу, чтобы они были, но третий - без проблем оставаясь влево. Как бы я сделал это правильно.
Ответы
Ответ 1
Макет крайне неэффективен и раздувается. Вам не нужно много LinearLayout
s. На самом деле вам вообще не нужен LinearLayout
.
Используйте только один RelativeLayout
. Вот так.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageButton android:background="@null"
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/back"
android:padding="10dip"
android:layout_alignParentLeft="true"/>
<ImageButton android:background="@null"
android:id="@+id/forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/forward"
android:padding="10dip"
android:layout_toRightOf="@id/back"/>
<ImageButton android:background="@null"
android:id="@+id/special"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/barcode"
android:padding="10dip"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Ответ 2
Вы можете сделать все это, используя только один RelativeLayout
(который, btw, не нуждается в параметре android:orientation
). Итак, вместо того, чтобы иметь LinearLayout
, содержащий кучу вещей, вы можете сделать что-то вроде:
<RelativeLayout>
<ImageButton
android:layout_width="wrap_content"
android:id="@+id/the_first_one"
android:layout_alignParentLeft="true"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_toRightOf="@+id/the_first_one"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Как вы заметили, некоторые параметры XML отсутствуют. Я просто показывал основные параметры, которые вы должны были поставить. Вы можете закончить все остальное.
Ответ 3
Это пример для RelativeLayout:
RelativeLayout relativeLayout=(RelativeLayout)vi.findViewById(R.id.RelativeLayoutLeft);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relativeLayout.setLayoutParams(params);
С другим видом макета (например, LinearLayout) вам просто нужно изменить RelativeLayout для LinearLayout.
Ответ 4
Если вы хотите использовать LinearLayout, вы можете выполнить выравнивание с layout_weight
с помощью элемента Space
.
например. следующие места размещения textView
и textView2
рядом друг с другом и textView3
будут выровнены по правому краю
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView2" />
<Space
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView3" />
</LinearLayout>
вы можете добиться такого же эффекта без Space
, если вы установите layout_weight
на textView2
. Просто мне нравится больше разделять, а также демонстрировать элемент Space
.
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView2" />
Обратите внимание, что вы должны (не обязательно) устанавливать layout_width
явно, поскольку он будет пересчитан в соответствии с этим весом в любом случае (так же, как вы должен установить высоту в элементах вертикальной LinearLayout
). Для других советов по настройке компоновки см. Android Layout Tricks.
Ответ 5
Для поддержки старой версии Space можно заменить View, как показано ниже. Добавьте это представление между оставленными большинством компонентов и перед правым большинством компонентов. Этот вид с весом = 1 будет растягиваться и заполнять пространство
<View
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="1" />
Здесь приведен полный код примера. Он имеет 4 компонента. Две стрелки будут справа и слева. Текст и Spinner будут посередине.
<ImageButton
android:id="@+id/btnGenesis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center_vertical"
android:layout_marginBottom="2dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="2dp"
android:background="@null"
android:gravity="left"
android:src="@drawable/prev" />
<View
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="1" />
<TextView
android:id="@+id/lblVerseHeading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="25sp" />
<Spinner
android:id="@+id/spinnerVerses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:gravity="center"
android:textSize="25sp" />
<View
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="1" />
<ImageButton
android:id="@+id/btnExodus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center_vertical"
android:layout_marginBottom="2dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="2dp"
android:background="@null"
android:gravity="right"
android:src="@drawable/next" />
</LinearLayout>