【问题标题】:Android TableRow TextView falls out of view on right sideAndroid TableRow TextView 在右侧消失
【发布时间】:2014-11-26 07:14:37
【问题描述】:

我有一个表格,用于显示一个名称后跟一个值。名称应出现在行的左侧,值应出现在右侧。这是一个例子:

这是我的XML 代码:

<TableRow>
    <TextView android:id="@+id/property_type_label"
        android:text="@string/property_type_label"
        android:padding="3dip" />
    <TextView android:id="@+id/property_type"
        android:text="@string/property_type"
        android:gravity="right"
        android:padding="3dip" />
</TableRow>

<View
    android:layout_height="2dip"
    android:background="#FF909090" />
</TableLayout>

问题是我无法查看与辅助TextViewTableRow 关联的字符串。理想情况下,存在一个不涉及大量手动padding 的解决方案。另外,android:layout_gravity="right" 也没有解决这个问题。

【问题讨论】:

    标签: android textview android-tablelayout gravity layout-gravity


    【解决方案1】:

    使用TextView的重力而不是带有权重的layout_gravity来分配每个TextView的一半布局

     <TableRow>
       <TextView android:id="@+id/property_type_label"
        android:text="@string/property_type_label"
        android:padding="3dip"
        android:layout_weight="1"
        android:gravity="left" />
    
        <TextView android:id="@+id/property_type"
        android:text="@string/property_type"
        android:layout_weight="1"
        android:gravity="right"/>
    </TableRow>
    

    【讨论】:

    • 谢谢!这是一个很好的简单实现,效果很好。
    • @BrianVanover,你有没有检查过我的编辑,实际上你不需要 android:layout_weight 或 android:gravity for property_type TextView。
    【解决方案2】:

    在文本视图中使用 layout_weight。试试这个

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
    <TableRow>
    <TextView android:id="@+id/property_type_label"
        android:text="@string/property_type_label"
        android:layout_weight="1" />
    
    
    <TextView android:id="@+id/property_type"
         android:text="@string/property_type"
        android:layout_weight="1" 
       />
     </TableRow>
    
    <View
       android:layout_height="2dip"
       android:background="#FF909090" />
    
     </TableLayout>
    

    希望这个答案对你朋友有帮助:)

    【讨论】:

      【解决方案3】:

      heightwidth 添加到 TableRow 并使用 weight,如下所示:

      <TableRow android:layout_width="match_parent"
                android:layout_height="match_parent">
              <TextView android:id="@+id/property_type_label"
                        android:text="@string/property_type_label"
                        android:padding="3dip" android:layout_weight="1"/>
              <TextView android:id="@+id/property_type"
                        android:text="@string/property_type"
                        android:gravity="right"
                        android:padding="3dip" android:layout_weight="1"/>
      </TableRow>
      

      【讨论】:

        【解决方案4】:

        尝试使用权重设置左右内容:

        <TableRow>
               <TextView
                   android:layout_width="0dp"
                   android:layout_weight="1"
                   android:layout_height="wrap_content"
                   android:id="@+id/property_type_label"
                   android:text="@string/property_type_label"/>
               <TextView
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"
                   android:id="@+id/property_type"
                   android:text="@string/property_type"/>
        </TableRow>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-18
          • 1970-01-01
          相关资源
          最近更新 更多