Android ImageButton分辨率问题 [英] Android ImageButton Resolution Problems
问题描述
我想在程序顶部有两个按钮,将用户带到不同的活动中.我在格式化方面遇到很多麻烦.
I want to have two buttons at the top of my program taking users to different activities. I'm having a lot of trouble with the formatting.
如何使按钮根据屏幕尺寸成比例地伸展?现在,对于一个屏幕尺寸,它们看起来会确定,然后我将切换到另一个屏幕尺寸,它将看起来全部被模糊或拉长.我已经尝试了所有不同的ScaleType,但似乎都没有什么不同.我还使用将所有图像按比例保存为img xhdpi,hdpi等正确尺寸Shubhayu的答案.
How can I make it so that the buttons will stretch proportionally based on the screen size? Right now, they will look OK for one screen size, then I will switch to a different one and it will appear all smushed or stretched. I've tried all of the different ScaleTypes and none seem to make a difference. I also went though and proportionally saved all of the images to the correct sizes regardimg xhdpi, hdpi, etc using Shubhayu's answer.
到目前为止,这是我的代码:
Here's my code so far:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/brushed_metal_background_dark"
tools:context=".HomeActivity" >
<ImageButton
android:id="@+id/incidentsSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/incident_bar2"
android:contentDescription="Incidents"
android:onClick="chooseIncident"
android:scaleType="center" />
<ImageButton
android:id="@+id/operationalPeriodsSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/operationalperiod_bar2"
android:contentDescription="Operational Periods"
android:onClick="chooseOperationalPeriod"
android:scaleType="fitCenter" />
推荐答案
将android:background
更改为android:src
将保持宽高比.使用android:scaleType="centerInside"
将整个图像容纳在按钮区域内,还可以选择使用android:adjustViewBounds=true
删除空白区域.示例:
Change android:background
to android:src
that will keep the aspect ratio. Use android:scaleType="centerInside"
to fit whole image inside button area and optionally use android:adjustViewBounds=true
to remove empty spaces. Example:
<ImageButton
android:id="@+id/incidentsSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="Incidents"
android:onClick="chooseIncident"
android:src="@drawable/incident_bar2"
android:scaleType="centerInside"
android:adjustViewBounds="true"/>
<ImageButton
android:id="@+id/operationalPeriodsSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="Operational Periods"
android:onClick="chooseOperationalPeriod"
android:src="@drawable/operationalperiod_bar2"
android:scaleType="centerInside"
android:adjustViewBounds="true"/>
这篇关于Android ImageButton分辨率问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!