sno*_*yak 7 android button autoresize android-layout
所以我有四个按钮,它们都使用match_parent来表示高度和宽度.我使用"drawableTop"属性在按钮本身内有图像,我想制作它,所以我不需要为每个分辨率单独的图像.这是我拍的肖像截图.景观将会有所改变.

所以这看起来很好,除了我只有4种不同的图像分辨率,并在我的手机上尝试它们没有产生积极的结果.有没有什么方法可以让我有一个更大的图像,比如只是"可绘制"并让它缩小以适应所有分辨率?如果没有,我该怎么办?我目前涵盖hdpi,ldpi,mdpi和xhdpi,但我想涵盖所有界限.还有哪些其他密度?
如果我可以实现自动调整大小,我也不必禁止用户使用Landscape:
另外,这是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>
Run Code Online (Sandbox Code Playgroud)
AFAIK,Button具有可绘制背景的情况并不容易,因为drawable不会随着视图缩放.
如果您将Buttons 更改为ImageButtons,我相信您可以这样做,这为缩放图像提供了更多选项.
ImageButton类公开android:scaleType可以使用的属性,centerInside缩小图像以适合ImageButton的边界.
还有ImageButton你应该用android:src而不是定义图像android:drawable.
我不确定是否有办法在ImageButton上设置文本.您可能需要一个更高级的布局,包括LinearLayout中的ImageView和TextView,然后将该LinearLayout视为按钮(添加onClick等).