Android LinearLayout - PullRequest
       16

Android LinearLayout

2 голосов
/ 09 июля 2009

Я хочу положить два TextView слева и одну кнопку справа внутри линейного макета, это возможно? Ниже приведен мой код, где мне пришлось жестко кодировать левый маркер кнопки, это негибко. Можно ли расположить детей в разных направлениях?

<LinearLayout
android:id="@+id/widget43"
android:layout_width="fill_parent"
android:layout_height="100px"
>
<TextView
android:id="@+id/tc_buttom_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time elapsed"
>
</TextView>
<TextView
android:id="@+id/tc_buttom_text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="00:00:00 00"
>
</TextView>
<Button
android:id="@+id/tc2_home"
android:layout_width="70px"
android:layout_height="wrap_content"
android:layout_marginLeft="200px"
android:layout_marginRight="10px"
android:text="Home"
android:layout_weight="0.5"
>
</Button>
</LinearLayout>

Ответы [ 6 ]

2 голосов
/ 09 июля 2009

Я хочу положить два TextView на слева и одна кнопка справа внутри линейного макета, это возможно

Не с одним LinearLayout. Вам нужно либо два LinearLayout с (один для столбца из двух TextView слева), либо один RelativeLayout.

Можно ли расположить детей, которые течет в разные стороны?

Если под «разными направлениями» вы подразумеваете как вертикальное, так и горизонтальное одновременно, один LinearLayout может идти только в одном направлении. Либо используйте вложенные LinearLayout s или RelativeLayout.

1 голос
/ 24 февраля 2012

Либо используйте две линейные компоновки (одну с горизонтальной ориентацией и другую с вертикальной ориентацией) или используйте относительную компоновку. Относительная компоновка прочнее линейной и проста в использовании

1 голос
/ 07 января 2010

Вам нужно использовать макет таблицы. посмотрите на пример компоновки таблицы в демонстрации API.

Table layout - with 'stretch columns' = 1,
 -- Table row  - with width = fill_parent,
    --  Text View,
    --  Text View,
    --  Button,

Это будет держать вашу правую кнопку нажатой к правому краю экрана

0 голосов
/ 05 июня 2018

Я использую две LinearLayouts, как это:

<?xml version="1.0" encoding="utf-8"?>
  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0099cc"
    tools:context=".FullscreenActivity">

    <!-- The primary full-screen view. This can be replaced with whatever view
         is needed to present your content, e.g. VideoView, SurfaceView,
         TextureView, etc. -->
    <TextView
        android:id="@+id/fullscreen_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:keepScreenOn="true"
        android:text="@string/dummy_content"
        android:textColor="#33b5e5"
        android:textSize="50sp"
        android:textStyle="bold" />

    <!-- This FrameLayout insets its children based on system windows using
         android:fitsSystemWindows. -->
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/background_dark"
        android:fitsSystemWindows="true">

        <LinearLayout
            android:id="@+id/fullscreen_content_controls"
            style="?metaButtonBarStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:background="@color/black_overlay"
            android:baselineAligned="false"
            android:orientation="vertical"
            tools:ignore="UselessParent">

            <LinearLayout
                android:id="@+id/fullscreen_choose_controls"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/fullscreen_esop"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="@string/vs_esop"
                    android:textAllCaps="false"
                    android:textColor="@android:color/holo_blue_dark"
                    android:textSize="50sp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/dummy_button"
                    style="?metaButtonBarButtonStyle"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="0.3"
                    android:ellipsize="end"
                    android:gravity="center"
                    android:text="@string/dummy_button"
                    android:textColor="@android:color/holo_red_dark"
                    android:textSize="20sp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/fullscreen_android"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_weight="1"
                    android:gravity="center"
                    android:text="@string/vs_android"
                    android:textAllCaps="false"
                    android:textColor="@android:color/holo_blue_dark"
                    android:textSize="50sp"
                    android:textStyle="bold" />

            </LinearLayout>

            <TextView
                android:id="@+id/fullscreen_make_your_choice"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:gravity="center"
                android:text="@string/vs_make_your_choice"
                android:textColor="@android:color/holo_red_dark"
                android:textSize="50sp"
                android:textStyle="bold" />

        </LinearLayout>
    </FrameLayout>

</FrameLayout>

Демо

0 голосов
/ 11 ноября 2015

Используйте следующее:

 <LinearLayout
            android:id="@+id/widget43"
            android:layout_width="fill_parent"
            android:layout_height="100px"
            android:layout_margin="16dp"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tc_buttom_text1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Time elapsed"></TextView>

                <TextView
                    android:id="@+id/tc_buttom_text2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="00:00:00 00"></TextView>
            </LinearLayout>

            <Button
                android:id="@+id/tc2_home"
                android:layout_width="70px"
                android:layout_height="wrap_content"
                android:layout_marginLeft="200px"
                android:layout_marginRight="10px"
                android:layout_weight="0.5"
                android:text="Home"></Button>
        </LinearLayout>

Дополнительные учебники и примеры для Android-макетов: http://www.viralandroid.com/2015/11/android-layouts.html

0 голосов
/ 09 июля 2009

LinearLayout имеет атрибут orientation. Попробуйте что-то вроде этого:

<LinearLayout
    android:id="@+id/widget43"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="100px"
>
...