【问题标题】:How to not stretch an image of a button in a table | ANDROID如何不拉伸表格中按钮的图像 |安卓
【发布时间】:2017-04-15 18:05:27
【问题描述】:

我有一个问题:

我有一个包含一行和两个单元格的表格。我把表格分成了一半的屏幕。每个单元格中都有一个具有自己的按钮样式(android:background)的按钮。每个按钮样式都包含一个图像。但问题是图像(按钮)被拉伸了。我不知道我必须更改什么才能使图像不再被拉伸。

这是表格布局:

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1">

                <Button
                    android:id="@+id/a"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_column="0"
                    android:layout_marginBottom="5dp"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/aa" />

            </FrameLayout>

            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1">

                <Button
                    android:id="@+id/b"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_column="1"
                    android:layout_marginBottom="5dp"
                    android:layout_marginLeft="5dp"
                    android:background="@drawable/bb" />

            </FrameLayout>

        </TableRow>
</TableLayout>

这里是按钮样式:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
       <item android:drawable="@drawable/aa1"
            android:state_pressed="true" />
       <item android:drawable="@drawable/aa2" />
 </selector>

我必须进行哪些更改才能使图像不被拉伸?图像的比例应该是可用的,并且应该被分割成一半的屏幕。

感谢您的帮助。这很重要。

【问题讨论】:

    标签: android image button stretch


    【解决方案1】:

    试试这个布局:使用ImageButton 代替Button 并将图像添加到android:src 属性中。

     <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1">
    
                <ImageButton
                    android:id="@+id/a"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_column="0"
                    android:layout_marginBottom="5dp"
                    android:layout_marginRight="5dp"
                    android:background="@android:color/transparent"
                    android:padding="15dp"
                    android:src="@mipmap/ic_launcher" />
    
            </FrameLayout>
    
            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="fill_parent"
                android:layout_weight="1">
    
                <ImageButton
                    android:id="@+id/b"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_column="1"
                    android:layout_marginBottom="5dp"
                    android:background="@android:color/transparent"
                    android:padding="15dp"
                    android:src="@mipmap/ic_launcher" />
    
            </FrameLayout>
    
        </TableRow>
    </TableLayout>
    

    输出:

    【讨论】:

      猜你喜欢
      • 2016-01-06
      • 2011-01-18
      • 2017-09-21
      • 2012-01-30
      • 2011-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-04
      相关资源
      最近更新 更多