2

我在 TableLayout/TableRow 中有一个 ImageButton。

我想将 ImageButton 分布在整个 TableRow 上并将图片设置在按钮的中心(高度像 tablerow/button 高度)

我在布局文件中设置...

 <TableLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent">


    <TableRow >

     <ImageButton
         android:id="@+id/CarPic"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"

         android:onClick="onButtonClick"
         android:scaleType="centerInside"
         android:src="@drawable/etype" />

 </TableLayout> 

当应用程序启动时,默认图片会像我描述的那样出现 - 但是当我使用 onclick 更改按钮的图片时...按钮调整为图片高度/宽度并停留在右上角

代码

Drawable d = Drawable.createFromPath(Path);
    LPic.setImageDrawable(d);   

我尝试了许多不同的版本:(填充和换行,但按钮会改变每张图片

4

3 回答 3

0

尝试这个:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
<ImageButton
    android:id="@+id/CarPic"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_gravity="center_horizontal"
    android:onClick="onButtonClick"
    android:src="@drawable/ic_launcher"
    android:layout_weight="1" />
 </TableRow>
 </TableLayout>
于 2013-08-19T08:54:49.953 回答
0

您可以将此代码用于高度和宽度

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TableRow
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1.0" >

    <ImageButton
        android:id="@+id/CarPic"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"
        android:onClick="onButtonClick"
        android:src="@drawable/ic_launcher" />
</TableRow>

于 2013-08-19T08:50:47.660 回答
0

使用此代码。您必须为此定义权重属性

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow>
    <ImageButton
        android:id="@+id/CarPic"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:onClick="onButtonClick"
        android:src="@drawable/ic_launcher"
        android:layout_weight="1.0" />
 </TableRow>
 </TableLayout>
于 2013-08-17T10:49:31.740 回答