2012-06-01 32 views
0

摘要:我想要一个水平的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> 

回答

0

不幸的是,最后我不得不以编程方式根据屏幕大小更改按钮大小。

我的最终布局看起来像这样(消毒):

<?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="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="0" 
     android:background="@null" 
     android:padding="2dip" 
     android:adjustViewBounds="true" 
     android:src="@drawable/logo" 
     android:scaleType="centerInside" 
      /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 

    <ImageButton 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:background="@null" 
     android:padding="2dip" 
     android:src="@drawable/button1" 
     android:scaleType="centerInside" 
     android:adjustViewBounds="true" 
     /> 

    <ImageButton 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:background="@null" 
     android:padding="2dip" 
     android:src="@drawable/button2" 
     android:scaleType="centerInside" 
     android:adjustViewBounds="true" 
      /> 

    <ImageButton 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:background="@null" 
     android:padding="2dip" 
     android:src="@drawable/button3" 
     android:scaleType="centerInside" 
     android:adjustViewBounds="true" 
      /> 

    <ImageButton 
     android:id="@+id/button4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     android:background="@null" 
     android:padding="2dip" 
     android:src="@drawable/button4" 
     android:adjustViewBounds="true" 
     android:scaleType="centerInside" 
      /> 

</LinearLayout> 

通知空观点,即填补了可用空间,推动其他按钮在屏幕的右侧。

在代码中,我检查屏幕大小。如果它看起来> = 7“,那么我切换到较大的图像 如果它似乎是> = 7”但< 9“,然后我以编程方式更改图像的大小 - 我必须试验如果我有更多的图片或他们改变了,我将不得不重复一遍,我并不为这种僵化的解决方案感到自豪,但我找不到任何可以工作的东西

0

这是我有:

在特定情况下,我们可以说,你的应用程序布局和按钮大小下dependecy:

  • 移动设备屏幕尺寸/解析度;
  • 行中的按钮数量;

我建议2种的方法来你:

  • 实现您的布局,RelativeLayout的重量标签。使用这些弹性布局可以非常容易;
  • 以编程方式使用DisplayMetrics类定义您的按钮尺寸,类似于本文后面的代码片段,并使用额外的.xml文件(比如整数)。xml),您可以在其中为您的按钮数量定义一个常数值;

在这个片段中,dm是一个保存您的设备分辨率的结构。

DisplayMetrics dm = new DisplayMetrics(); 
context.getWindowManager().getDefaultDisplay().getMetrics(dm); 

希望它可以帮助你! :)

+0

我对你使用RelativeLayout和weight的建议有点困惑你的意思是使用LinearLayout和layout_weight?我不知道有任何RelativeLayout的重量标签 至于在程序上根据屏幕更改按钮大小大小,这可能是必要的,它只是一个丑陋而不必要的破解,但如果我找不到更好的选择,我可能会陷入困境。 –