Android ImageButton:Force square图标(高度=包裹内容,宽度=?)

Android ImageButton:Force square图标(高度=包裹内容,宽度=?),android,button,imagebutton,Android,Button,Imagebutton,在我的水平线布局中,我有一个文本编辑和一个图像按钮。ImageButton与TextEdit一样高 我希望ImageButton的宽度和长度一样 此时,ImageButton的宽度看起来就像没有缩放时一样(ImageButton宽度[px]=未缩放可绘制宽度[px]): 它应该是什么样子的: 您可以使用px调整图像按钮的大小。。。就像下面 `android:layout_width="5px" android:layout_height="5px"` 只需使用权重和来相应

在我的水平线布局中,我有一个文本编辑和一个图像按钮。ImageButton与TextEdit一样高

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

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


它应该是什么样子的:


您可以使用px调整图像按钮的大小。。。就像下面

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

只需使用权重和来相应地划分控件的大小

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

希望它能帮助您。

使用

android:scaleType="fitCenter"
或者
android:scaleType=“centerInside”


在xml文件中的ImageButton中。

试试这个,我认为应该可以:

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


解释:
中心内部
将确保图像在
图像按钮
的范围内按比例缩放
adjustViewBounds=“true”
将…如果需要缩放图像,请调整视图的边界。

根据您的需要,您的
ImageButton
是可伸缩的,与它的长度一样宽,最好使用它。您可以在此处找到帮助表单&

尝试添加

adjustViewBounds=“true”

对于ImageButton,应剪裁多余的宽度




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

android:layout_gravity="center|clip_horizontal"

希望这能奏效

只是遇到了类似的情况,而其他建议没有帮助。帮助的是在ImageButton中设置填充:

android:padding="5dp"

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

我自己试过,它可以工作

只需执行以下操作

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

对于
EditText
ImageButton


我参加聚会可能会晚一点。 但是,有一种通过重写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>
和使用

        //(2)if you want the width to match the height
        //super.onMeasure(heightMeasureSpec, heightMeasureSpec);
我也有同样的问题。 我试着创造出这样的东西:

但我得到的是:

ImageButton
被水平拉伸。
所有最重要的答案对我都不起作用。但我注意到有人提到了
layou-weight
,出于好奇,我查了一下,发现了以下内容:

布局权重

LinearLayout还支持使用android:layout\u weight属性为单个子级分配权重。此属性根据视图在屏幕上应占据的空间大小为视图指定“重要性”值。较大的权重值允许其展开以填充父视图中的任何剩余空间。子视图可以指定一个权重值,然后视图组中的任何剩余空间都将按其声明权重的比例指定给子视图。默认权重为零

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

因此,基本上,如果您将元素的
布局\u宽度设置为0,它将根据其内容的维度显示。
如果将其设置为其他值,元素将在包含它的父元素中争夺额外空间;更多加权元素占用更多空间

因此,在我的示例中,当我为
TextView
ImageButton
都设置
layout\u width=0
时,它们都不会占用任何额外的空间,并且都会挤到左边

但是当我为
TextView
将其设置为1,为
ImageButton
将其设置为0时,
ImageButton
不会占用超出其内容所需的任何空间;而
TextView
会占用所有额外的空间,并向右按
ImageButton

正是我想要的方式。

最初,两个元素都被设置为默认的
layout\u weight
为1,因此它们都在平等地争夺额外的空间。

我很确定他希望它能够精确缩放;)(但是,问题陈述中没有说明。)谢谢你的建议。如果应用程序在小屏幕上运行,我可能需要25%或30%的空间将按钮显示为正方形。这个问题也有解决办法吗?谢谢你的建议。我应该为
android:layout\u width
android:layout\u height
使用哪些值?您可以根据自己的要求使用填充父级或包装内容。。。!!!如果您满意,请检查答案。。。!!!谢谢你的建议。不幸的是,它不能正常工作(截图:)。你知道其他解决方案吗?我编辑了我的答案,试试看。另外,你能上传你正在使用的按钮吗(如果这不起作用的话)?谢谢更新的答案。它似乎还不起作用(截图:)。我的可绘制(pin2):为可绘制链接获取502(坏网关)。不幸的是,我找不到一个优雅的解决方案。然而,我确实找到了解决办法。我已经用解决方案编辑了我的答案。对不起,它没有你希望的那么漂亮。谢谢你的建议。不幸的是,它不能正常工作(截图:)。您知道其他解决方案吗?请尝试删除标签
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);
    }
}
<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>
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        //(2)if you want the width to match the height
        //super.onMeasure(heightMeasureSpec, heightMeasureSpec);