1

I'm trying to build an android application and I've created a screen with a table layout and added to the first table row an image view and two large texts, to the second row I've added one button.
When I change the text of the button it automatically changes the size of the image view above to match the size of the button.

Here is the code:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ChildInfoActivity" >

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

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:contentDescription="@string/child_info_image_descreption" />

    <TextView
        android:id="@+id/childInfoNameTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/name_text_view"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_gravity="center_vertical" />

    <TextView
        android:id="@+id/childInfoHasArrivedTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/has_arrived_text_view"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_gravity="center_vertical" />

</TableRow>

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

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/more_info_button"
        android:layout_weight="1" />

</TableRow>

why is that?

4

4 回答 4

1

原因是使用 时TableLayoutTableRow每个相同列的大小都相同。由于ImageViewandButton都在第 0 列,所有影响Button' 宽度的东西也会影响ImageView' 宽度。

如果您允许Button跨越许多列,请添加android:layout_span="2"Button. 否则,使用不同的布局,例如LinearLayoutor RelativeLayout

于 2013-11-08T08:59:09.553 回答
0

你的按钮有 android:layout_width="wrap_content". 如果您设置 android:layout_width="100dp" ,那么您的宽度将不是动态的。

于 2013-11-08T08:34:23.383 回答
0

因为包含 imageView 和 textViews 的行项具有“wrap_content”值作为高度。因此,当您的文本高度大于您的图像视图时,它可能也会增加其父高度。

您可以尝试将 imageView 和 textViews 放在 relativeLayout 中,并用 imageViews 高度包装 textViews,然后将该 relativeLayout 放入行项中。它将是嵌套的,但我认为它仍然比给定的确切尺寸要好,因为这样在某些设备上就不会那么好...

于 2013-11-08T08:38:10.040 回答
0

remove android:layout_gravity="center_vertical" and as u have given added android:layout_weight="1" to button add this line to other components also

于 2013-11-08T08:46:31.750 回答