【问题标题】:imagebutton becomes very large when I choose my avatar当我选择我的头像时,imagebutton 变得非常大
【发布时间】:2018-02-06 20:10:59
【问题描述】:

我有一个固定在导航菜单右上角的图像按钮,当您单击它时,您可以从您的 android 库中选择一个图像并将其作为您的头像。

但是,一旦您选择了图片,图片按钮就会变得非常大。

这是按钮最初的样子:Before Choosing avatar

这是选择头像后按钮的样子:After choosing avatar

我在 NavDrawerActivity 类的 onCreate() 方法中插入了这段代码

imgButton = (ImageButton) navigationHeaderView.findViewById(R.id.imageButton1);
imgButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");

        startActivityForResult(intent, IMAGE_UPLOAD_REQUEST);
    }
});

RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), getThumbnail("desiredFilename.png"));
roundedBitmapDrawable.setCircular(true);
imgButton.setImageDrawable(roundedBitmapDrawable);

这是我的 XML:

<ImageButton
   android:layout_gravity="right"
   android:id="@+id/imageButton1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:background="@drawable/round_button"
   android:scaleType="fitXY"
   app:srcCompat="@android:drawable/ic_menu_gallery"/>

【问题讨论】:

    标签: java android xml android-studio layout


    【解决方案1】:

    尝试将其添加到 XML 中以设置固定大小:

    android:maxWidth="42dp"  
    android:maxHeight="42dp"
    

    42dp只是例子,随便设置

    【讨论】:

    • 我用 5 和 20 都试过了,但结果是一样的
    【解决方案2】:

    从您给我们的工作来看,头像的图像尺寸比原始图标大得多。如果您希望它与图标的大小完全相同,您可以缩小图像,或将ImageButton 的大小从 wrap_content 更改为固定值。 :)

    改变这个:

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

    到这里:

    android:layout_width="THE WIDTH OF THE ICON"  
    android:layout_height="THE HEIGHT OF THE ICON"
    

    编辑: 有点抽象的想法,但不妨尝试一下。

    改变这个:

    <ImageButton
                android:layout_gravity="right"
                android:id="@+id/imageButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/round_button"
                android:scaleType="fitXY"
                app:srcCompat="@android:drawable/ic_menu_gallery"/>
    

    到这里:

    <ImageButton
                android:layout_gravity="right"
                android:id="@+id/imageButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/round_button"
                app:srcCompat="@android:drawable/ic_menu_gallery"/>
    

    【讨论】:

    • 我从未有过 maxWidth 和 maxHeight。你是说layout_width吗?
    • 是的,对不起,我的错。
    • 我现在明白了……很奇怪。不知道是什么问题,但感谢您尝试...imgur.com/a/OKJyB
    • 这很奇怪。请参阅我的编辑以了解更多步骤。
    • 不走运,只是把圆变成了正方形。
    猜你喜欢
    • 2010-10-07
    • 1970-01-01
    • 2011-12-23
    • 2018-10-03
    • 2014-01-24
    • 1970-01-01
    • 2015-01-27
    • 2019-06-10
    • 2020-11-11
    相关资源
    最近更新 更多