Как правильно выровнять виджет в горизонтальной линейной компоновке андроида?
Это код, который я использую, и он не работает:
<?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="fill_parent"
android:orientation="horizontal">
<TextView android:text="TextView" android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right">
</TextView>
</LinearLayout>
Ответы
Ответ 1
Попробуйте добавить пустой вид внутри горизонтального LinearLayout перед элементом, который вы хотите увидеть правильно, например:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Ответ 2
Не меняйте гравитацию LinearLayout на "right", если вы не хотите, чтобы все было справа
Try:
- Изменить TextView width на
fill_parent
-
Изменить TextView гравитация на right
<TextView
android:text="TextView"
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right">
</TextView>
Ответ 3
В качестве дополнения к ответу alcsan вы можете использовать Space
, поскольку здесь описывается документ API 14 (Android 4.0 ICE_CREAM_SANDWICH), .
Прост - это легкий подкласс View, который может использоваться для создания разрывов между компонентами в макетах общего назначения.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:gravity="right" />
</LinearLayout>
Для приложений, поддерживающих уровни API до 14 лет, существует поддержка android.support.v4.widget.Space
с помощью библиотеки поддержки Android r22.1.0.
Ответ 4
С Линейная компоновка
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/select_car_book_tabbar"
android:gravity="right" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/my_booking_icon" />
</LinearLayout>
![enter image description here]()
с FrameLayout
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/select_car_book_tabbar">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:src="@drawable/my_booking_icon" />
</FrameLayout>
с RelativeLayout
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/select_car_book_tabbar">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:src="@drawable/my_booking_icon" />
</RelativeLayout>
Ответ 5
настройка представления layout_weight="1"
выполнит трюк.!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Ответ 6
Добавьте android:gravity="right"
в LinearLayout. Предполагая, что TextView
имеет layout_width="wrap_content"
Ответ 7
просто добавьте android:gravity="right"
в макет Liner.
Ответ 8
Вы должны использовать RelativeLayout и просто перетаскивать их, пока они не выглядят хорошо:)
<ImageView
android:id="@+id/button_info"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:contentDescription="@string/pizza"
android:src="@drawable/header_info_button" />
</RelativeLayout>
Ответ 9
Попробуйте изменить layout_width на android:layout_width="match_parent"
, потому что gravity:"right"
выравнивает текст внутри layout_width, и если вы выберете содержимое обложки, у него нет места, куда нужно идти, но если вы выберете match parent, он может пойти вправо.
Ответ 10
Я сделал это самым простым способом:
Просто выберите один RelativeLayout и разместите в нем дочерний вид, который вы хотите разместить в справа сторона.
<LinearLayout
android:id="@+id/llMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f5f4f4"
android:gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="20dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="20dp">
<ImageView
android:id="@+id/ivOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/txtOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Hiren"
android:textAppearance="@android:style/TextAppearance.Medium"
android:textColor="@android:color/black" />
<RelativeLayout
android:id="@+id/rlRight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right">
<ImageView
android:id="@+id/ivRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
</LinearLayout>
Надеюсь, это поможет вам.
Ответ 11
linear layout
с layout_width="fill_parent"
, а также виджет с тем же layout width
+ gravity as right
будет выравнивать его вправо.
Я использую 2 TextView
в следующем примере, topicTitle
слева и topicQuestions
справа.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:orientation="horizontal">
<TextView
android:id="@+id/topicTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/topicQuestions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
Выход
![ANf9L.png]()
Ответ 12
В случае с TextView:
<TextView
android:text="TextView"
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:textAlignment="gravity">
</TextView>
Ответ 13
Вот пример. клавиша для размещения выглядит следующим образом
android:layout_width="0dp"
android:layout_weight="1"
Полный код
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:id="@+id/categoryName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="abcd" />
<TextView
android:id="@+id/spareName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="efgh" />
</LinearLayout>
![введите описание изображения здесь]()
Ответ 14
Попробуйте это.
<?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="fill_parent"
android:orientation="horizontal"
android:gravity="right" >
<TextView android:text="TextView" android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>