ImageButton:强制方形图标(高度= WRAP_CONTENT,宽度=?)

时间:2012-03-24 14:35:08

标签: android button imagebutton

在我的水平LinearLayout中,我有一个TextEdit和一个ImageButton。 ImageButton与TextEdit一样高。

我希望ImageButton的长度和它一样宽。

目前看起来像ImageButton的宽度就像没有缩放时一样(ImageButton width [px] =未缩放的可绘制宽度[px]):

example

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

它应该如何:

objective

12 个答案:

答案 0 :(得分:21)

试试这个,我认为这应该有效:

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

说明: centerInside将确保图片在ImageButton的范围内按比例缩放。如果需要缩放图像,adjustViewBounds="true"将......嗯,调整视图的边界。

答案 1 :(得分:5)

尝试添加

  

adjustViewBounds = “真”

到ImageButton,它应该剪掉多余的宽度


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

答案 2 :(得分:2)

使用

android:scaleType="fitCenter"

android:scaleType="centerInside"

在xml文件的ImageButton中。

答案 3 :(得分:1)

我可能有点迟到了。 但是,通过重写onMeasure()可以轻松实现此行为。 以下是它的样子:

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);
    }
}

然后,您只需使用此自定义替换XML的ImageButton:

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

您只需将wrap_content放到宽度或高度,具体取决于您想要指定按钮大小的那个。 如果您希望按钮将其高度包裹到图像中,并且宽度只是与高度匹配,那么您可以使用

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

并使用

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

答案 4 :(得分:1)

我遇到了同样的问题。 我试图创建一个看起来像这样的东西: enter image description here

但我得到的是这个:
enter image description here

ImageButton水平拉伸 所有最佳答案都不适合我。但是我注意到有人提到layout_weight,只是出于好奇而查找了它,并在Android docs上找到了以下内容:

  

布局重量

     

LinearLayout还支持使用android:layout_weight属性为各个孩子分配权重。此属性根据应在屏幕上占用多少空间为视图指定“重要性”值。较大的权重值允许它扩展以填充父视图中的任何剩余空间。子视图可以指定权重值,然后视图组中的任何剩余空间将按其声明权重的比例分配给子项。默认权重为零。

     

例如,如果有三个文本字段,其中两个声明权重为1,而另一个没有权重,则没有权重的第三个文本字段不会增长,只会占用其内容所需的区域。在测量所有三个场之后,另外两个将平均扩展以填充剩余的空间。如果第三个字段的权重为2(而不是0),那么它现在被宣布为比其他字段更重要,因此它获得剩余空间总量的一半,而前两个共享其余部分

所以基本上,如果你为一个元素设置layout_width为0,它将根据其内容的尺寸显示。
如果你将它设置为其他任何东西,该元素将争夺包含它的父元素中的额外空间;更多加权元素占用更多空间。

因此,当我在我的示例中为layout_width = 0TextView设置ImageButton时,它们都没有占用任何额外的空间,而且它们都蜷缩在左边。

enter image description here

但是当我将TextView设置为1而ImageButton设置为0时,ImageButton不会占用其内容所需的空间;而TextView占用了所有额外空间,并将ImageButton推向右侧 就是我想要的方式。 enter image description here

最初,发生的事情是两个元素都设置为默认layout_weight为1,因此两者都同样竞争额外的空间。 enter image description here

答案 5 :(得分:0)

只需使用weightS sum来相应地划分控件的大小......

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

希望它会对你有所帮助。

答案 6 :(得分:0)

如你所愿ImageButton是可拉伸的&amp;正如它的长度一样宽,最好使用NinePatch Image。您可以在此处找到帮助Draw 9-patch&amp; How does Android’s nine-patch tool work ?

答案 7 :(得分:0)

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

答案 8 :(得分:0)

只需将以下行添加到ImageButton中,额外的背景就会消失:

android:layout_gravity="center|clip_horizontal"

希望这有效

答案 9 :(得分:0)

刚刚碰到这样的事情,其他建议没有帮助。帮助是在ImageButton中设置填充:

android:padding="5dp"

除此之外,我没有触及原始布局。按钮在我测试的3个仿真器上变成了正方形(3.2,4.4.2,5.1)

答案 10 :(得分:0)

我试过自己,它有效

只需执行以下操作...

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

LinearLayout EditTextImageButton

$('div[id^="myModal_"] span').on('click', function() { $(this).parent().parent().parent().parent().hide(); }) 内使用布局重量比5:1

OUTPUT

答案 11 :(得分:-3)

您可以使用px调整图像按钮的大小......如下所示......

    `android:layout_width="5px"
    android:layout_height="5px"`
相关问题