ImageButton: Force square icon (height = WRAP_CONTENT, width = ?)

吃可爱长大的小学妹 提交于 2019-12-05 22:44:05

问题


In my horizontal LinearLayout I have a TextEdit and an ImageButton. The ImageButton is as high as the TextEdit.

I'd like that the ImageButton is exactly as wide as it's long.

At the moment it looks like the width of the ImageButton is like when there is no scaling (ImageButton width [px] = unscaled drawable width [px]):

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/txtName"
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/btSet"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:scaleType="fitEnd"
        android:src="@drawable/pin2" />

</LinearLayout>

How it should look like:


回答1:


Try this, I think this should work:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/btSet"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:scaleType="centerInside"
        android:adjustViewBounds="true"
        android:src="@drawable/pin2" />

    <EditText
        android:id="@+id/txtName"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@id/btSet" />

</RelativeLayout>

Explaination: centerInside will assure that the image will scale proportionally within the bounds of the ImageButton. adjustViewBounds="true" will...well, adjust the view's bounds, if the image needed to be scaled.




回答2:


try adding

adjustViewBounds="true"

to the ImageButton, that should clip the excess width


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/txtName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ImageButton
        android:id="@+id/btSet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:src="@drawable/pin" />
</LinearLayout>



回答3:


Use

android:scaleType="fitCenter"

or android:scaleType="centerInside"

in the ImageButton in xml File.




回答4:


I might be a bit late to the party. However there's an easy way to achieve this behavior by overriding the onMeasure(). Here's how it'd look like :

public class MySquareImageButton extends ImageButton {
    public MySquareImageButton(Context context) {
        super(context);
    }

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

    public MySquareImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //(1)if you want the height to match the width
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
        //(2)if you want the width to match the height
        //super.onMeasure(heightMeasureSpec, heightMeasureSpec);
    }
}

And then you'd simply replace your XML's ImageButton with this custom one :

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/txtName"
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <com.whatever_your_package.MySquareImageButton
        android:id="@+id/btSet"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:scaleType="fitEnd"
        android:src="@drawable/pin2" />

</LinearLayout>

You'd simply put wrap_content to width or height, depending on which one you want to dictate the size of your button. In the case you want your button to wrap its height to the image, and that the width simply matches the height, you'd use

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

and use

        //(2)if you want the width to match the height
        //super.onMeasure(heightMeasureSpec, heightMeasureSpec);



回答5:


I had the same problem. I was trying to create something that looked like this:

But what I was getting was this:

The ImageButton was getting stretched horizontally.
All the top answers didn't work for me. But I noticed people mentioning layout_weight and just looked it up out of curiosity and found the following on Android docs:

Layout Weight

LinearLayout also supports assigning a weight to individual children with the android:layout_weight attribute. This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view. Child views can specify a weight value, and then any remaining space in the view group is assigned to children in the proportion of their declared weight. Default weight is zero.

For example, if there are three text fields and two of them declare a weight of 1, while the other is given no weight, the third text field without weight will not grow and will only occupy the area required by its content. The other two will expand equally to fill the space remaining after all three fields are measured. If the third field is then given a weight of 2 (instead of 0), then it is now declared more important than both the others, so it gets half the total remaining space, while the first two share the rest equally

So basically, if you set the layout_width to be 0 for an element, it'll appear according to the dimensions of its content.
If you set it to anything else, the element will fight for extra space in the parent element that contains it; with more weighted elements taking up more space.

So, when I set layout_width = 0 for both TextView and ImageButton in my example, neither of them takes up any extra space and they both huddle up to the left.

But when I set it to 1 for TextView and 0 for ImageButton, the ImageButton doesn't take any more space than required by its content; while the TextView takes up all the extra space and pushes ImageButton to the right.
Just the way I want it.

Initially, what had happened was both the elements were set to have a default layout_weight of 1 and hence both were equally competing for the extra space.




回答6:


Simply use the weightSum to devide the size of controls accordingly...

<?xml version="1.0" encoding="utf-8" ?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" android:layout_width="fill_parent"
      android:layout_height="fill_parent">

   <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:weightSum="1.0" >

   <EditText
       android:id="@+id/txtName"
       android:layout_weight="0.8"
       android:layout_height="wrap_content"
       android:layout_width="0dp" 
       android:inputType="text"
       android:singleLine="true"/>

   <ImageButton
       android:id="@+id/btSet"
       android:layout_weight="0.2"
       android:layout_height="wrap_content"
       android:layout_width="0dp"
       android:scaleType="fitEnd"
       android:src="@drawable/pin2" />

  </LinearLayout>
</LinearLayout>

Hope it will help you.




回答7:


as you want your ImageButton is stretchable & exactly as wide as it's long, its better to use NinePatch Image. you may find help form here Draw 9-patch & How does Android’s nine-patch tool work ?




回答8:


<LinearLayout
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:weightSum="1.0" >

<EditText
   android:id="@+id/txtName"
   android:layout_weight="0.75" // this work like percentage adjust as u want 70 or 75
   android:layout_height="wrap_content"
   android:layout_width="0dp" 
   android:inputType="text"
   android:singleLine="true"/>

<ImageButton
   android:id="@+id/btSet"
   android:layout_weight="0.25" // this work like percentage adjust as u want 25 or 30
   android:layout_height="wrap_content"
   android:layout_width="0dp"
   android:scaleType="fitEnd"
   android:src="@drawable/pin2" />

</LinearLayout>



回答9:


Just add the following line to your ImageButton and the extra background will fade away:

android:layout_gravity="center|clip_horizontal"

hope this works




回答10:


Just ran into something like this and the other suggestions didn't help. What did help was setting the padding in the ImageButton:

android:padding="5dp"

Besides that, I didn't touch the original layout. The button became square on the 3 emulators that I tested (3.2, 4.4.2, 5.1)




回答11:


I tried myself,it works

Simply do the Following...

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    <EditText
        android:id="@+id/editText1"
        android:layout_width="0dp"
        android:layout_weight="5"
        android:layout_height="wrap_content"
        android:inputType="text"/>

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent" />

</LinearLayout>

Use Layout Weight ratio 5:1 inside a LinearLayout for EditText and ImageButton




回答12:


You Can resize the Image button using px... like below...

    `android:layout_width="5px"
    android:layout_height="5px"`


来源:https://stackoverflow.com/questions/9852568/imagebutton-force-square-icon-height-wrap-content-width

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!