具有复杂组件放置的 Android 表格布局

发布于 2024-12-15 17:28:39 字数 4647 浏览 2 评论 0原文

已编辑:我自己找到了解决方案。谢谢!

我想为 Android 应用程序创建以下屏幕: 理想屏幕

现在我有这个: 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"

谢谢!!

EDITED: I HAVE FOUND THE SOLUTION MYSELF. THANK YOU!

I want to create the following screen for an Android App:
ideal screen

Right now I have this:
current dev

with this code:

<?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>

The text can be aligned to the right using android:gravity="right", but how can I make it
centered vertically?

--> AISHH, it's: android:gravity="center_vertical|right"

Thank you!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我爱人 2024-12-22 17:28:39

那么 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 -->

Well TableLayout is not a good ideea. You should try using a RelativeLayout and some LinearLayout nested inside.

Try something like this:

<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 -->

白云悠悠 2024-12-22 17:28:39

我做了一个类似的布局,如下所示:

<?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>

那里有一个滚动面板,按钮始终位于屏幕底部,以防您的配置更长。

I've done a similar layout like this:

<?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>

There's a scroll panel in there to have the buttons on the bottom of the screen at all times, in case your configuration goes longer.

如日中天 2024-12-22 17:28:39

这就是我想出的实现。不是最好的,但对我有用:)

<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>

So this is the implementation I came up with. Not the best but worked for me :)

<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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文