Android xml中的ImageButton(图像太小,分辨率太差)

emi*_*ily 3 android imagebutton android-linearlayout

我有3个图像按钮的以下内容.代码如下所示,三个按钮看起来相同.问题:

  1. 图像未完全填充按钮的大小
  2. 图像的分辨率很差.我将图像保存在所有res/drawable文件夹中(drawable-hdpi,mdpi等).图像由"Android Icon Set"向导加载.

有任何想法吗?我一直在玩padding和scaleType,因为我把它们视为其他帖子的解决方案.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_marginTop="10dp"
    android:orientation="vertical">

<ImageButton
    android:id="@+id/abutton"
    android:src="@drawable/a_button_icon"
    android:layout_height="150dp" 
    android:layout_width="150dp" 
    android:padding="5dp"
    android:scaleType="fitXY"/>

<ImageButton
    android:id="@+id/button"        
    android:src="@drawable/b_button_icon"
    android:layout_height="150dp" 
    android:layout_width="150dp" 
    android:scaleType="fitXY"/>

 </LinearLayout>

 <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:layout_marginTop="10dp"> 

<ImageButton
    android:id="@+id/cbutton"        
    android:src="@drawable/d_icon"
    android:layout_height="150dp" 
    android:layout_width="150dp" 
    android:padding="20dp"
    android:scaleType="fitXY"/>

<ImageButton
    android:id="@+id/dbutton"        
    android:src="@drawable/about_a_button"
    android:layout_height="150dp" 
    android:layout_width="150dp" 
    android:padding="20dp"
    android:scaleType="fitXY"/>

    </LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

jag*_*han 7

而不是将drawable android:src赋值给android:background 属性,而是将其赋值给ImageButton.

要么

您可以尝试设置android:adjustViewBounds="true",这将ImageButton调整其边界以保持其drawable的纵横比

PS:如果将图像设置为ImageButton的背景,则图像将缩放到ImageButton的任何大小.除此之外,src是前景图像,背景是背景图像.

编辑:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <!-- The two linear layouts will have equal widths. -->
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:padding="5dp" >

        <!-- The two image button will fill the linear layout along width and have height 150dp -->
        <ImageButton
            android:id="@+id/abutton"
            android:layout_width="fill_parent"
            android:layout_height="150dp"
            android:background="@null"
            android:scaleType="fitXY"
            android:src="@drawable/engage_down" />

        <ImageButton
            android:id="@+id/button"
            android:layout_width="fill_parent"
            android:layout_height="150dp"
            android:layout_marginTop="10dp"
            android:background="@null"
            android:scaleType="fitXY"
            android:src="@drawable/engage_down" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        android:padding="5dp" >

        <ImageButton
            android:id="@+id/cbutton"
            android:layout_width="fill_parent"
            android:layout_height="150dp"
            android:background="@null"
            android:scaleType="fitXY"
            android:src="@drawable/engage_down" />

        <ImageButton
            android:id="@+id/dbutton"
            android:layout_width="fill_parent"
            android:layout_height="150dp"
            android:layout_marginTop="10dp"
            android:background="@null"
            android:scaleType="fitXY"
            android:src="@drawable/engage_down" />
    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.