禁用宽度继承?

时间:2014-12-02 23:39:28

标签: java android xml

我正在为学校开发一个基本的Android应用程序,并且我在布局方面遇到了一些问题。我正在使用包含多行的表格布局。

我遇到的问题是物品的宽度。当我在页面的顶部或底部添加按钮时,其他行中的文本视图似乎继承了按钮宽度。我需要这些文本视图是狭义的,关于单个字符的大小。似乎项目宽度设置为表格中最宽的项目。我尝试了几个房产,但没有运气。

任何指导都将不胜感激。

这是一个截图 -

As you can see, the textViews are inheriting the width of the start button

这是我的代码 -

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="fill"
android:padding="20dp"
android:shrinkColumns="1" >

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start" />

</TableRow>

<TableRow
    android:id="@+id/tableRow2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"            
        android:text=""         
        android:height="50dp"
        android:width="50dp" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:width="50dp"
        android:height="50dp" />

</TableRow>

<TableRow
    android:id="@+id/tableRow3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:width="50dp"
        android:height="50dp" />
    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:width="50dp"
        android:height="50dp" />

</TableRow>

1 个答案:

答案 0 :(得分:2)

根据您希望特定视图跨越的列数,只需将layout_span添加到您的视图中。

例如,在您的代码中:

<TableRow
    android:id="@+id/tableRow1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"
        android:layout_span="2" />

</TableRow>

然后为每个TextView提供layout_span的1。