android在tablerow中设置imagebutton

时间:2013-08-17 10:40:29

标签: android center imagebutton

我在TableLayout / TableRow中有一个ImageButton。

我想将ImageButton传播到完整的TableRow并设置图片 在按钮的中心(高度像桌子/按钮高度)

我在layoutfile中设置了......

 <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> 

当应用程序启动时,默认图片就像我描述的那样 - 但当我改变时,点击按钮的图片... 按钮调整到pic高度/宽度,并保持在右上角

代码

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

我尝试了很多不同的版本:(填充和包装但是 按钮改变每张图片

3 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>

答案 2 :(得分: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>
相关问题