简体   繁体   English

在LinearLayout中自动调整ImageButtons的大小

[英]automatically resize ImageButtons in LinearLayout

Summary: I want a horizontal row of ImageButtons to scale down evenly to fit in the screen size. 简介:我希望水平排列的ImageButtons均匀缩小以适合屏幕尺寸。

I have a row of ImageButtons at the top of the screen. 我在屏幕顶部有一排ImageButtons。 A left-aligned logo ImageButton and then right-aligned ImageButtons for various actions. 左对齐徽标ImageButton,然后右对齐ImageButtons以执行各种操作。

Yes, that does sound a lot like a Honeycomb/ICS Action Bar, but the code is targeted for Froyo, so I need to implement the design in 2.2-compatible layouts. 是的,听起来确实像Honeycomb / ICS操作栏,但是代码是针对Froyo的,因此我需要在2.2兼容的布局中实现设计。

I programmatically detect when I'm on a 7" screen or larger and switch to larger images for the ImageButtons (as the drawable-xlarge directory only works for 10" and up, and drawable-large works for 4" and up). 我以编程方式检测我何时处于7英寸或更大的屏幕上,并为ImageButtons切换到更大的图像(因为drawable-xlarge目录仅适用于10英寸及以上,而drawable-large适用于4英寸及以上)。

This works great on a phone or on a 10" tablet. However, on a 7" tablet, the images are too large for the space in portrait mode (the app is locked to portrait mode). 这在手机或10英寸平板电脑上效果很好。但是,在7英寸平板电脑上,图像在纵向模式下无法容纳空间(该应用程序已锁定为纵向模式)。

I have tried many different approaches to making the images scale down, as I'd rather not use yet another set of images just for 7" tablets. But the images are not spaced properly, or scale at different levels, or only some of the images appear on the screen. I've used RelativeLayout, LinearLayout, android:weightSum, setting the images as background images, as src images, etc. 我尝试了许多不同的方法来缩小图像,因为我不想只为7英寸的平板电脑使用另一组图像。但是图像的间距不正确,或者缩放比例不同,或者只有一部分图像出现在屏幕上,我使用了RelativeLayout,LinearLayout,android:weightSum,将图像设置为背景图像,src图像等。

EDIT: Another quirk I noticed in my experimentation was that if I set the same layout_weight, the layout worked, forcing each item to have the same width. 编辑:我在实验中注意到的另一个奇怪之处是,如果我设置相同的layout_weight,则布局有效,迫使每个项目都具有相同的宽度。 If I want some items to have different widths--which is necessary in this case, as the first button needs to be substantially wider--then the layout breaks when I set a layout_weight that doesn't match the others. 如果我希望某些项目具有不同的宽度(在这种情况下这是必要的,因为第一个按钮需要实质上更宽),那么当我设置的layout_weight与其他项目不匹配时,布局就会中断。 Only one or two of the items appear on screen, the rest presumable being pushed off. 屏幕上仅出现一个或两个项目,其余的可能被推下。

Here's the layout I'm using. 这是我正在使用的布局。 This is my best so far--it works great on 10" tablets and phones, and is almost tolerable on 7" tablets. 到目前为止,这是我最好的-在10英寸平板电脑和手机上效果很好,在7英寸平板电脑上几乎可以忍受。 But the logo--which should be the same height as the buttons and about 2.5 times wider--is noticeably taller than the other buttons. 但是徽标-应该与按钮相同的高度,大约是按钮的2.5倍-明显比其他按钮高。 Any suggestions on improving the layout? 对改善布局有什么建议吗?

<?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>

Unfortunately, in the end I did have to programmatically change the button sizes depending on the size of the screen. 不幸的是,最后我不得不根据屏幕大小以编程方式更改按钮的大小。

My final layout looked like this (sanitized): 我的最终布局看起来像这样(已清理):

<?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>

Notice the empty View that fills up available space, pushing the other buttons over to the right side of the screen. 请注意,空视图填充了可用空间,将其他按钮推到屏幕右侧。

In the code, I check the screen size. 在代码中,我检查了屏幕尺寸。 If it seems to be >= 7", then I switch to larger images. If it seems to be >=7" but < 9", then I programmatically change the size of the images--and that I had to experiment with to come up with just the right number for it to work. If I had more images or they changed, I would have to repeat it. I'm not proud of such an inflexible solution, but I couldn't find anything else that worked. 如果它看起来> = 7“,那么我切换到更大的图像。如果它看起来>> 7”但<9“,那么我将以编程方式更改图像的大小,而我不得不尝试提出正确的编号才能工作。如果我有更多图像或更改了图像,我将不得不重复一遍。我为这种灵活的解决方案不感到骄傲,但找不到其他可行的方法。

This is what I've got: 这就是我得到的:

In your specific case, we can say that your app layout and buttons size are under dependecy of: 在您的特定情况下,我们可以说您的应用布局和按钮大小取决于以下因素:

  • The mobile device screensize/resolution; 移动设备的屏幕尺寸/分辨率;
  • The number of buttons in the row; 该行中的按钮数;

I recommend 2 approaches to you: 我向您推荐2种方法:

  • Implementing your layout with RelativeLayout and weight tags. 使用RelativeLayoutweight标签实现布局。 Flexible layouts can be made very easy with these ones; 这些布局可以使布局变得非常容易。
  • Programatically define your button sizes using DisplayMetrics class, something like the snippet in the end of this post, and use an extra .xml file (say integer.xml) where you can define a constant value for your number of buttons ; 使用DisplayMetrics类以编程方式定义按钮大小(类似于本文结尾的代码段),并使用一个额外的.xml文件(例如integer.xml),您可以在其中为按钮数量定义一个常量值;

In this snippet, dm is a structure that holds your device resolutions. 在此代码段中, dm是用于保存设备分辨率的结构。

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

Hope it helped you! 希望对您有所帮助! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM