1

Помогите новичку. Не могу разобраться: как расположить Button в самом низу LinearLayout, а TextView - слева?

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="60dp"
    android:layout_weight="0.05">
</Button>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp">
</TextView>
1
  • Что значит слева?
    – Kota1921
    18 фев 2015 в 20:07

3 ответа 3

4

Я бы сделал так:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:layout_gravity="center_horizontal|bottom" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:layout_gravity="left|top"
    android:textColor="#000" />
</FrameLayout>

А если нужно именно через линейный лейаут, то так:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textColor="#000" />
<View
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="New Button"
    android:gravity="bottom" />
</LinearLayout>

alt text

4

Используйте RelativeLayout. Он позволяет разместить компоненты относительно друг друга и относительно самого RelativeLayout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:layout_alignParentBottom="true">
    </Button>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_alignParentLeft="true">
    </TextView>
</RelativeLayout>
3
  • Этак можно получить наползание (перекрытие) одного элемента на другой.
    – ЮрийСПб
    18 фев 2015 в 22:00
  • @ЮрийСПб, а можнол и не получить. А можно получить и другим способом. Это зависит от того, откуда руки растут. 19 фев 2015 в 6:07
  • @metalurgus, не, оно понятно, конечно... Только вот, думаю, это - наиболее простой способ выстрелить себе в ногу в данной ситуации))
    – ЮрийСПб
    19 фев 2015 в 6:49
1

Помимо RelativeLayout, коий "тяжелее" LinearLayout, можно вложить Button и TextView в другие LinearLayout, коим можно назначить свойство Gravity.

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="left">
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</TextView>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:orientation="vertical"
    android:gravity="bottom">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
 </Button>
 </LinearLayout>
 </LinearLayout>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.