【问题标题】:android tablelayout with complex component placement具有复杂组件放置的android tablelayout
【发布时间】:2011-12-29 07:28:58
【问题描述】:

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

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

现在我有这个:

使用此代码:

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

谢谢!!

【问题讨论】:

  • 使用 2 LinerLayout 垂直方向并相应地设置 layout_weight

标签: android android-linearlayout tablelayout


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我做了一个类似的布局:

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-16
        • 2010-12-01
        • 2019-06-30
        • 1970-01-01
        • 1970-01-01
        • 2018-02-08
        相关资源
        最近更新 更多