摘要:我想要一个水平的ImageButton行按比例缩小以适应屏幕大小。在LinearLayout中自动调整ImageButton的大小
我在屏幕顶部有一排ImageButton。左对齐的徽标ImageButton,然后右对齐的ImageButtons进行各种操作。
是的,这听起来很像Honeycomb/ICS Action Bar,但代码是针对Froyo的,所以我需要在2.2兼容的布局中实现该设计。
我以编程方式检测到我在7英寸或更大的屏幕上并切换到ImageButtons的较大图像(因为drawable-xlarge目录仅适用于10“及以上,可绘制的大型作品适用于4”以及)
这在手机或10“平板电脑上效果很好。但是,在7英寸的平板电脑上,图片对于纵向模式中的空间太大(应用程序被锁定为纵向模式)。
我尝试了很多不同的方法来缩小图像的尺寸,而不是仅用于7英寸平板电脑的另一组图像。但图像间隔不合适,或者在不同层次上缩放,或者只有一些图像出现在屏幕上。我使用了RelativeLayout,LinearLayout,android:weightSum,将图像设置为背景图像,作为src图像等。
编辑:我在实验中注意到的另一个怪癖是如果我设置相同的layout_weight, ,强迫每个项目具有相同的宽度。如果我想要一些项目具有不同的宽度 - 在这种情况下这是必要的,因为第一个按钮需要大得多 - 那么当我设置与其他项目不匹配的layout_weight时,布局会中断。只有一个或两个项目出现在屏幕上,其余的可能推出。
这是我使用的布局。这是我迄今为止最好的 - 它在10英寸平板电脑和手机上效果很好,而在7英寸平板电脑上几乎可以忍受。但徽标 - 与按钮高度相同,宽度大约为2.5倍 - 明显高于其他按钮。任何关于改善布局的建议?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/actionBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:padding="5dip"
android:gravity="center_vertical"
android:orientation="horizontal"
android:background="@drawable/action_bar_bg"
>
<ImageButton
android:id="@+id/logoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:src="@drawable/save_logo_03"
android:padding="2dip"
android:adjustViewBounds="true"
android:scaleType="centerInside"
/>
<View
android:id="@+id/spacer"
android:layout_width="50dip"
android:layout_height="1dip"
android:layout_weight="1"
android:visibility="invisible"
/>
<ImageButton
android:id="@+id/listButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/list_button"
android:background="@null"
android:adjustViewBounds="true"
android:padding="2dip"
android:scaleType="centerInside"
/>
<ImageButton
android:id="@+id/mapButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:src="@drawable/map_button2"
android:adjustViewBounds="true"
android:padding="2dip"
android:scaleType="centerInside"
/>
<ImageButton
android:id="@+id/ltoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:src="@drawable/lto_button"
android:adjustViewBounds="true"
android:padding="2dip"
android:scaleType="centerInside"
/>
<ImageButton
android:id="@+id/searchButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:src="@drawable/search_button"
android:adjustViewBounds="true"
android:padding="2dip"
android:scaleType="centerInside"
/>
</LinearLayout>
我对你使用RelativeLayout和weight的建议有点困惑你的意思是使用LinearLayout和layout_weight?我不知道有任何RelativeLayout的重量标签 至于在程序上根据屏幕更改按钮大小大小,这可能是必要的,它只是一个丑陋而不必要的破解,但如果我找不到更好的选择,我可能会陷入困境。 –