【问题标题】:Aligning components in a TableRow layout在 TableRow 布局中对齐组件
【发布时间】:2013-11-01 13:50:06
【问题描述】:

我正在尝试创建一个与 facebook android 应用程序有点相似的标题。 header 中间应该有一个 header 标题,两边各有一个按钮。

我有标题,但我下面的代码没有显示任何一个按钮。我不确定此信息是否有帮助,但此标题的 TableRow 布局占用了大量高度空间。在我尝试添加按钮之前,它会一直包裹直到标题的高度。

<TableLayout 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"
tools:context=".MainActivity" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="#008000" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/menuButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/HeaderTextView"
            android:text="Button" />

        <TextView
            android:id="@+id/HeaderTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/header"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:gravity="center"
            android:textColor="#FFF"
            android:textStyle="italic" />

        <Button
            android:id="@+id/infoButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/HeaderTextView"
            android:text="Button" />

    </RelativeLayout>

</TableRow>

</TableLayout>

【问题讨论】:

    标签: android xml


    【解决方案1】:

    这对我有用。无论出于何种原因,android:layout_weight="1" 都能正常工作。

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
    
            <Button
                android:id="@+id/menuButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="Button" />
    
            <TextView
                android:id="@+id/HeaderTextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:text="This is the title"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#FFFfff"
                android:textStyle="italic" />
    
            <Button
                android:id="@+id/infoButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="Button" />
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:
      <TableLayout 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"
        tools:context=".MainActivity" >
      
      
      <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="center_vertical" >
      
      <Button
          android:id="@+id/menuButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:text="Button" />
      
      <TextView
          android:id="@+id/HeaderTextView"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerInParent="true"
          android:layout_toRightOf="@id/menuButton"
          android:layout_toLeftOf="@+id/infoButton"
          android:text="headersfasfsdfsdfsdfsdfsdf"
          android:textColor="@android:color/black"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:gravity="center"
          android:textStyle="italic" />
      
      <Button
          android:id="@+id/infoButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:text="Button" />
      
      </RelativeLayout>
      
      </TableLayout>
      

      【讨论】:

      • 感谢您的回答!这有所帮助,但目前,按钮紧贴标题。我想要靠近屏幕左右边缘的按钮。我应该使用哪个命令?