Android Tablelayout со сложным размещением компонентов - PullRequest
1 голос
/ 15 ноября 2011

РЕДАКТИРОВАНИЕ: Я НАШЕЛ РЕШЕНИЕ ДЛЯ СЕБЯ. СПАСИБО!

Я хочу создать следующий экран для приложения Android: ideal screen

Прямо сейчас у меня есть это: current dev

с этим кодом:

<?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:weightSum="1" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_weight=".65"
        android:weightSum="5" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="TOURNAMENT: "
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_weight="1"
            android:layout_marginLeft="10dp"/>
        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="GAME: "
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_weight="1"
            android:layout_marginLeft="10dp" />
        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="BEST OF: "
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_weight="1"
            android:layout_marginLeft="10dp" />
        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="BRACKET STYLE: "
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:layout_weight="1"
            android:layout_marginLeft="10dp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_weight=".35"
        android:weightSum="5" >

        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="10dp" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="10dp" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="10dp" />
        <EditText
            android:id="@+id/editText1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginRight="10dp" />

        <RelativeLayout
            android:id="@+id/linearLayout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_weight="1" >

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="15dp"
                android:text="CANCEL" />

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@+id/button2"
                android:text="CREATE" />

        </RelativeLayout>

</LinearLayout>

</LinearLayout>

Текст можно выровнять по правому краю, используя android: gravity = "right", но как я могу это сделать по центру вертикально?

-> AISHH, это: android: gravity = "center_vertical | right"

Спасибо !!

Ответы [ 3 ]

1 голос
/ 15 ноября 2011

Я сделал похожий макет, как это:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"
android:layout_height="fill_parent" android:layout_width="fill_parent"
>
    <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0">
        <LinearLayout android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="fill_parent">
            <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:stretchColumns="1" android:shrinkColumns="1">
                <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
                    <TextView android:text="Value1" android:gravity="right" />
                    <EditText android:singleLine="true" android:id="@+id/editTextVal1" />
                </TableRow>
                <TableRow android:layout_width="fill_parent" android:layout_height="wrap_content">
                    <TextView android:text="Long name value2" android:gravity="right" />
                    <EditText android:singleLine="true" android:id="@+id/editTextVal1" />
                </TableRow>
            </TableLayout>
        </LinearLayout>
    </ScrollView>
    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save" />
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel" />
    </LinearLayout>
</LinearLayout>

Там есть панель прокрутки, чтобы кнопки всегда были внизу экрана, на случай, если ваша конфигурация будет длиться дольше.

1 голос
/ 15 ноября 2011

Ну, TableLayout не очень хорошая идея. Вы должны попытаться использовать RelativeLayout и несколько LinearLayout, вложенных внутрь.

Попробуйте что-то вроде этого:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:id="@+id/first_linear"
        android:orientation="vertical">
        <!-- Note that for text view,and editText you might need to use certain values (dip values) cuz wrap content will try to arrange the view -->
        <TextView
            android:id="@+id/first_text_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <EditText
            android:id = "@+id/first_edit_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>
   <!-- we align all the following linearlayouts below the first -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/first_linear"
        android:id="@+id/second_linear"
        android:orientation="vertical">
        <TextView
            android:id="@+id/second_text_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <EditText
            android:id = "@+id/second_edit_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </LinearLayout>

<!-- add all the layouts like those from above -->

<!-- ...... -->
<!-- after you added all the fields you can add a linear layout containing two buttons to float below the last one -->

0 голосов
/ 15 ноября 2011

Так что это реализация, которую я придумал. Не самый лучший, но у меня сработало:)

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_weight=".65"
    android:weightSum="5"
    android:paddingTop="10dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="TOURNAMENT: "
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_weight="1"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical|right"/>
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="GAME: "
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_weight="1"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical|right"/>
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="BEST OF: "
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_weight="1"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical|right"/>
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="BRACKET STYLE: "
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_weight="1"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical|right"/>

    <!-- fill up the space so that the components on top are displayed correctly -->

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_weight="1"
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical|right"/>
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_weight=".35"
    android:weightSum="5"
    android:paddingTop="10dp" >

    <EditText
        android:id="@+id/tourName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginRight="10dp"
        android:gravity="center_vertical|left" />

    <Spinner
        android:id="@+id/spinGameName"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginRight="10dp"
        android:gravity="center_vertical|left" />

    <Spinner
        android:id="@+id/spinBestOf"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginRight="10dp"
        android:gravity="center_vertical|left" />

    <Spinner
        android:id="@+id/spinBracketType"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginRight="10dp"
        android:gravity="center_vertical|left" />

    <RelativeLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="1" >

        <Button
            android:id="@+id/btnCancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="15dp"
            android:text="CANCEL" />

        <Button
            android:id="@+id/btnCreate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/btnCancel"
            android:text="CREATE" />

    </RelativeLayout>

</LinearLayout>

...