Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 有没有办法让按钮上的图像自动调整大小以适应屏幕?_Android_Android Layout_Button_Autoresize - Fatal编程技术网

Android 有没有办法让按钮上的图像自动调整大小以适应屏幕?

Android 有没有办法让按钮上的图像自动调整大小以适应屏幕?,android,android-layout,button,autoresize,Android,Android Layout,Button,Autoresize,所以我有四个按钮,所有的按钮在高度和宽度上都使用match_parent。我使用“drawableTop”属性在按钮本身中有图像,我希望这样做,这样我就不需要为每个分辨率分别创建图像。这是我拍的一张人像截图。景观一号稍后会出现 这看起来不错,但我只有4种不同的图像分辨率,在我的手机上试用并没有产生积极的效果。有没有什么方法可以让我只需要一张更大的图片,比如说“可绘制”,然后让它缩小到适合所有分辨率?如果没有,我该怎么办?我目前涵盖hdpi、ldpi、mdpi和xhdpi,但我想涵盖所有边界。还

所以我有四个按钮,所有的按钮在高度和宽度上都使用match_parent。我使用“drawableTop”属性在按钮本身中有图像,我希望这样做,这样我就不需要为每个分辨率分别创建图像。这是我拍的一张人像截图。景观一号稍后会出现

这看起来不错,但我只有4种不同的图像分辨率,在我的手机上试用并没有产生积极的效果。有没有什么方法可以让我只需要一张更大的图片,比如说“可绘制”,然后让它缩小到适合所有分辨率?如果没有,我该怎么办?我目前涵盖hdpi、ldpi、mdpi和xhdpi,但我想涵盖所有边界。还有其他密度吗

如果我能实现自动调整大小,我也不必禁止用户使用景观:

此外,以下是xml:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:baselineAligned="false"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        tools:ignore="NestedWeights" >

        <Button
            android:id="@+id/bMap"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/ic_camera"
            android:text="Button" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <Button
            android:id="@+id/bCamera"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/ic_camera"
            android:text="Button" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:baselineAligned="false"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical"
        tools:ignore="NestedWeights" >

        <Button
            android:id="@+id/bGallery"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/ic_camera"
            android:text="Button" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <Button
            android:id="@+id/bPlants"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawableTop="@drawable/ic_camera"
            android:text="Button" />
    </LinearLayout>
</LinearLayout>



好吧,使用带有可绘制背景的
按钮不容易实现,因为可绘制的不能随视图缩放

如果您将
按钮
s更改为
ImageButton
s,我相信您可以做到这一点,这为缩放图像提供了更多选项

ImageButton类公开了
android:scaleType
属性,通过该属性可以使用
centerInside
,它将缩小图像以适应ImageButton的边界

同样,使用
ImageButton
您应该使用
android:src
而不是
android:drawable
来定义图像


但我不确定是否有办法在ImageButton上设置文本。您可能需要一个更高级的布局,包括线性布局中的ImageView和TextView,然后将该线性布局视为按钮(添加onClick等)。

是的,您可以有一个更大的图像,并将其缩小以适应所有分辨率。您可以通过编程实现自动调整大小。看看DroidUtils.scaleButtonDrawables。

是的,我研究了这个,发现scaleType适合我的需要,但似乎没有办法在图像按钮上添加文本,就像我在上图中看到的那样,对吧/@背包头-不,恐怕不行。你可能会发现这个问题很有用:@TIm-是的,我就是在那里学会做drawableTop的。看起来我不能完全做我需要做的事情,但也许线性布局选项可能是我需要走的路线。我会等待其他的解决方案,但现在你的似乎是最好的。谢谢@蒂姆-你的方法非常有效。我将标准按钮xml用于LinearLayout的背景绘制,因此它可以获得所有的动画。谢谢