37

我有 5 个方形 ImageButton,我想在屏幕底部并排排列。我将每一组(不同的ID)设置为:

        <ImageButton
         android:id="@+id/box1" 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content" 
         android:layout_gravity="center"
         android:adjustViewBounds="true"
         android:scaleType="fitXY"
         android:layout_weight="1"
         android:layout_margin="1dp"
         /> 

我在主java中分配了这样的背景:

    int[] imageIds = new int[] {R.id.box1,R.id.box2,R.id.box3,R.id.box4,R.id.box5};
    for(int i = 0; i<5; i++){
        imageButtons[i] = (ImageButton) findViewById(imageIds[i]);
        imageButtons[i].setBackgroundResource(R.drawable.blank);
    }

我想要它做的是缩放宽度以整齐地并排放置在屏幕底部(现在可以了),但高度也会自动缩放以匹配宽度。这可能吗?我不想使用 setImageSource,因为它会在 imagebutton 周围放置一个边框。

4

9 回答 9

34
   <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:id="@+id/layoutButtons">

     <com.package.SquareButton         
        android:layout_height="fill_parent"
        android:layout_width="0dip"          
        android:layout_weight="1"

        <ImageView
        android:id="@+id/box1"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"
        android:layout_height="wrap_content"
        android:layout_width="0dp"          
        android:layout_weight="1" 
        android:layout_marginLeft="5dp" 
        android:layout_marginRight="5dp"/>

      </com.package.SquareButton>

      <com.package.SquareButton         
        android:layout_height="fill_parent"
        android:layout_width="0dip"          
        android:layout_weight="1"

        <ImageView
        android:id="@+id/box2"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"          
        android:layout_marginLeft="5dp" 
        android:layout_marginRight="5dp"/>

</com.package.SquareButton>

   .........          
 </LinearLayout>

然后添加这个自定义按钮类:

public class SquareButton extends LinearLayout {

    public SquareButton(Context context) {
        super(context);
    }

    public SquareButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    // This is used to make square buttons.
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }
}
于 2011-11-02T10:50:37.853 回答
33

在尝试将按钮排成一排时,我也遇到了类似的头痛。我找到的解决方案是结合使用ImageButtonandroid:src属性(setImageSource在代码中)android:background="@null"

据我了解,图像按钮的背景不会受到影响adjustViewBounds,它只是imageView你设置的android:src

然后默认行为是给你一个方形按钮,中间有 imageView。您可以通过将背景设置为 来覆盖它@null,这样您就只剩下图像了。

然后,您可以使用 aLinearLayout或 table 来布局您的按钮。我在 XML 中完成了所有操作,并使用以下布局在屏幕底部创建了一排两个按钮,这些按钮可按比例放大或缩小,以保持不同设备屏幕尺寸的纵横比。希望这可以帮助。

<TableLayout    
    android:id="@+id/tableLayout2"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginBottom="20dip"
    android:stretchColumns="*">

    <TableRow>

     <ImageButton
        android:id="@+id/button_one"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="0.5"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"  
        android:onClick="clickOne"
        android:background="@null"
        android:src="@drawable/button_one_drawable" />

    <ImageButton
        android:id="@+id/button_two"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:layout_weight="0.5"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"  
        android:onClick="clickTwo"
        android:background="@null"
        android:src="@drawable/button_two_drawable" />

    </TableRow>      
</TableLayout>
于 2011-11-01T13:00:47.190 回答
12

代替

android:scaleType="fitXY" 

采用:

android:scaleType="centerInside"

EDIT1:试试这个:

    <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/layoutToInflateButtons"
            >
    <ImageButton 
        android:id="@+id/box1"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"          
        android:layout_weight="1"          
        android:layout_margin="1dp"
        />          
    </LinearLayout>
于 2011-01-11T11:55:33.053 回答
3

您可以使用 Google 的百分比相对布局来帮助您管理视图纵横比。

你可以像这样使用它

     <android.support.percent.PercentRelativeLayout
             xmlns:android="http://schemas.android.com/apk/res/android"
             xmlns:app="http://schemas.android.com/apk/res-auto"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

         <ImageButton
             android:id="@+id/box1" 
             android:layout_width="fill_parent"
             android:layout_height="wrap_content" 
             android:layout_gravity="center"
             android:adjustViewBounds="true"
             android:scaleType="fitXY"
             app:layout_aspectRatio="100%"
             android:layout_weight="1"
             /> 
     </android.support.percent.PercentFrameLayout>

纵横比 100% 将使宽度与高度相同。

例子:

android:layout_width="300dp"
app:layout_aspectRatio="178%"

宽度为高度的 178%。这是 layout_aspectRatio 期望的格式

你可以在这里详细阅读

于 2016-08-03T13:19:40.813 回答
1

我确定您希望拥有 5 个宽度/高度相等的按钮。如果是这种情况,请使用 LinearLayout 并将所有这 5 个按钮layout_width="0dip"layout_weight="1"

例如:

<LinearLayout
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:id="@+id/layoutButtons">

    <ImageButton 
        android:id="@+id/box1"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"
        android:layout_height="wrap_content"
        android:layout_width="0dip"          
        android:layout_weight="1"/>

    <ImageButton 
        android:id="@+id/box2"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:scaleType="centerInside"
        android:layout_height="wrap_content"
        android:layout_width="0dip"          
        android:layout_weight="1"/>    

   .........          
 </LinearLayout>
于 2011-10-31T06:35:08.793 回答
1

在检查了今年的 Google I/O 示例应用程序后,我发现 Google 正在使用尺寸值来根据屏幕类型指定不同的高度或宽度。有关详细信息,您可以从http://code.google.com/p/iosched/查看源代码。

您可以在 values-xhdpi 或 values-sw720dp 中指定按钮的高度,例如:

 <resources>
    <dimen name="button_height">50dp</dimen>
</resources>

然后你可以在指定高度时使用这个维度值:

android:layout_height="@dimen/button_height"
于 2012-10-06T22:39:54.417 回答
0

将 ImageButtons 的宽度设置为fill_parent并使用 scaletypefitStart用于紧靠左边距的图像和右边的图像fitEnd。至少就您的示例图像而言,应该做到这一点。如果图像的比例宽度超过屏幕宽度,您可能会遇到一些间距问题,但它应该适合您。

于 2011-11-04T13:44:09.277 回答
0
ViewGroup.LayoutParams params = imageButtonName.getLayoutParams();
// Changes the height(or width) and width(or height) to the specified 
params.height = layout.getWidth();
于 2013-09-21T08:56:22.733 回答
0

研究制作自定义 ImageButton。我喜欢这个,因为它是一类。这是一个根据图像纵横比调整其高度的按钮。我想你可以调整你的需求:

public class AspectImageButton extends ImageButton {


public AspectImageButton(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    this.getDrawable();

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int width = getMeasuredWidth();

    Drawable d=this.getDrawable(); //grab the drawable

    float fAspectRatio=(float)d.getIntrinsicWidth()/(float)d.getIntrinsicHeight();

    float fHeight=(float)width/fAspectRatio;//get new height

    setMeasuredDimension(width, (int)fHeight);
}

public AspectImageButton(Context context, AttributeSet attrs) {
    super(context, attrs);

    // TODO Auto-generated constructor stub
}

public AspectImageButton(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    // TODO Auto-generated constructor stub
}

}

然后在 xml 中像这样使用它:

<com.package.AspectImageButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/home_1b"
                android:background="@null"
                android:scaleType="fitXY"
                android:adjustViewBounds="true" />
于 2015-05-20T16:45:46.623 回答