【问题标题】:How to use a custom layout as RadioButton label如何使用自定义布局作为 RadioButton 标签
【发布时间】:2020-02-23 14:37:04
【问题描述】:

我为RadioButton 创建了一个我想实现的自定义布局。 android类的代码在这里:

public class MyRadioButton extends LinearLayout implements View.OnClickListener{
    private ImageView iv;
    private TextView tv;
    private RadioButton rb;

    private View view;

    public MyRadioButton(Context context) {
        super(context);
        view = View.inflate(context, R.layout.my_radio_button, this);
        setOrientation(HORIZONTAL);

        rb = (RadioButton) view.findViewById(R.id.radioButton1);
        tv = (TextView) view.findViewById(R.id.textView1);
        iv = (ImageView) view.findViewById(R.id.imageView1);

        view.setOnClickListener(this);
        rb.setOnCheckedChangeListener(null);
    }

    public void setImageBitmap(Bitmap bitmap) {
        iv.setImageBitmap(bitmap);
    }

    public View getView() {
        return view;
    }

    @Override
    public void onClick(View v) {

        boolean nextState = !rb.isChecked();

        LinearLayout lGroup = (LinearLayout)view.getParent();
        if(lGroup != null){
            int child = lGroup.getChildCount();
            for(int i=0; i<child; i++){
                //uncheck all
                ((RadioButton)lGroup.getChildAt(i).findViewById(R.id.radioButton1)).setChecked(false);
            }
        }

        rb.setChecked(nextState);
    }

    public void setImage(Bitmap b){
        iv.setImageBitmap(b);
    }

    public void setText(String text){
        tv.setText(text);
    }

    public void setChecked(boolean isChecked){
        rb.setChecked(isChecked);
    }
}

布局的代码在这里

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="top"
        android:text="" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/wow_visa_prepaid" />

    </LinearLayout>
</LinearLayout>

目前,我不知道如何将继承从 LinearLayout 更改为 RadioButton 并保持相同的布局。

这个应该是圆角

【问题讨论】:

  • RadioButton 必须有作为父 RadioGroup
  • 你能说得更具体些吗
  • 您必须在 RadioGroup 中使用单选按钮。请改用复选框。
  • 我在单选按钮组中使用单选按钮,但我必须为单选按钮自定义视图布局
  • 你必须从 RadioButton 扩展你的类

标签: android android-layout radio-button android-custom-view


【解决方案1】:

有两种方法可以完成这项工作:

1.当我们听说自定义视图时,它驱使我们重写 onDraw 方法,然后将我们想要的内容绘制到视图的 Canvas 中。

2.在这种情况下,有一种更简单的方法,使用drawableLeft。这里我扩展了AppCompatRadioButton,并将考虑的布局设置为drawableLeft


MyRadioButton.java

package com.aminography.radiobutton;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;

import jp.wasabeef.glide.transformations.MaskTransformation;

// TODO: If you are using androidx
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatRadioButton;

// TODO: If you are using appcompat
//import android.support.annotation.Nullable;
//import android.support.v7.widget.AppCompatRadioButton;

public class MyRadioButton extends AppCompatRadioButton {

    private View view;
    private TextView textView;
    private ImageView imageView;

    public MyRadioButton(Context context) {
        super(context);
        init(context);
    }

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

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

   private RequestListener<Bitmap> requestListener = new RequestListener<Bitmap>() {
        @Override
        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
            return false;
        }

        @Override
        public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
            imageView.setImageBitmap(resource);
            redrawLayout();
            return false;
        }
    };

    public void setImageResource(int resId) {
        Glide.with(getContext())
                .asBitmap()
                .load(resId)
                .apply(RequestOptions.bitmapTransform(
                        new MultiTransformation<>(
                                new CenterCrop(),
                                new RoundedCornersTransformation(dp2px(getContext(), 24), 0, RoundedCornersTransformation.CornerType.ALL))
                        )
                )
                .listener(requestListener)
                .submit();
    }

    public void setImageBitmap(Bitmap bitmap) {
        Glide.with(getContext())
                .asBitmap()
                .load(bitmap)
                .apply(RequestOptions.bitmapTransform(
                        new MultiTransformation<>(
                                new CenterCrop(),
                                new RoundedCornersTransformation(dp2px(getContext(), 24), 0, RoundedCornersTransformation.CornerType.ALL))
                        )
                )
                .listener(requestListener)
                .submit();
    }

    // setText is a final method in ancestor, so we must take another name.
    public void setTextWith(int resId) {
        textView.setText(resId);
        redrawLayout();
    }

    public void setTextWith(CharSequence text) {
        textView.setText(text);
        redrawLayout();
    }

    private void init(Context context) {
        view = LayoutInflater.from(context).inflate(R.layout.my_radio_button_content, null);
        textView = view.findViewById(R.id.textView);
        imageView = view.findViewById(R.id.imageView);
        redrawLayout();
    }

    private void redrawLayout() {
        view.setDrawingCacheEnabled(true);
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

        view.buildDrawingCache(true);
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
        setCompoundDrawablesWithIntrinsicBounds(new BitmapDrawable(getResources(), bitmap), null, null, null);
        view.setDrawingCacheEnabled(false);
    }

    private int dp2px(Context context, int dp) {
        return (int) (dp * context.getResources().getDisplayMetrics().density);
    }

}

my_radio_button_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="96dp"
        android:layout_height="64dp"
        android:src="@drawable/img_visa" />

</LinearLayout>


视觉效果:


注意:

1.如果您在项目中使用appcompat,请在类顶部注释androidx 导入并取消注释appcompat 之一。

2.您只需将android:paddingLeft更改为RadioButton即可更改自定义布局的位置:

<com.aminography.radiobutton.MyRadioButton
    android:id="@+id/radioButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="8dp" />



已编辑:

我已经使用GlideGlide-Transformations 重写了代码以解决圆角图像的要求。

build.gradle

dependencies {
    implementation 'com.github.bumptech.glide:glide:4.9.0'
    implementation 'jp.wasabeef:glide-transformations:3.3.0'
}

【讨论】:

  • 几乎是完美的。单选按钮图标应与 textview 对齐,但我认为我可以使用 top|left 重力轻松完成
  • 是的,将android:gravityandroid:paddingTop 用于RadioButton,可以对齐它们。
  • 我在重力方面取得了成功,但我还有一个问题。我需要图像始终具有圆角。 ImageView roundedImageView = containerLL.findViewById(R.id.roundedImageView); roundedImageView.setClipToOutline(true); Bitmap bitmap = AppUtil.decodeSampledBitmapFromResource(new File(item.getImagePath()), width, height); roundedImageView.setImageBitmap(bitmap); roundedImageView.setBackgroundResource(R.drawable.rounded_corner); 当我使用 MyRadioButton 时,此代码不起作用
  • 请告诉我decodeSampledBitmapFromResource 代码。
  • 我需要R.drawable.rounded_corner?我想你想用rounded_corner 形状掩盖图像。我说的对吗?
【解决方案2】:

有很多方法可以做到这一点,例如,您可以使用以下库

【讨论】:

  • 库不是我需要的,见上图。
【解决方案3】:

1. 当您创建类时,扩展 LinearLayout 或 FrameLayout,.xml 布局文件(my_radio_button_content.xml), 必须以“merge”而不是“LinearLayout”开头,否则 将是 LinearLayout(your class) 中的 LinearLayot(.xml)。

<merge>
    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="top"
        android:text="" />

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/wow_visa_prepaid" />
    </LinearLayout>
</merge>

2. 很容易创建:final ArrayList&lt;MyRadioButton&gt;,然后通过 给所有的孩子,如果你愿意,兄弟们会改变每一个 其他人。

public MyRadioButton(Context context) {
   super(context);
   inflate(context, R.layout.my_radio_button, this); 
}

public void SetMyViewClickable(final ArrayList<String> brothers) {
    setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            for (View brother:brothers) {
                ((RadioButton)brother.findViewById(R.id.radioButton1)).setChecked(false);
            }
        }
    });
}

【讨论】:

  • 我想避免这种设置 myViewClickable 的行为。我希望 RadioGroup 自动处理点击。
  • 你不能,因为它的自定义视图和 radioButton 比你的实际视图小。 RadioGroup 仅将单选按钮作为子视图处理。
  • 确实,出于这个原因,我需要扩展 RadioButton 类并仅更改单选按钮内部的布局。
【解决方案4】:

我已经尝试了以下代码,它运行良好,没有错误:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".MainActivity">

<com.example.myapplication.MyRadioButton
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

更改以下代码后:

public MyRadioButton(Context context) {

转至以下代码:

 public MyRadioButton(Context context, AttributeSet attributes) {

这是您需要的解决方案吗?

【讨论】:

  • 不,不是。我需要保留问题描述中的上述布局,但必须将public class MyRadioButton extends LinearLayout 更改为public class MyRadioButton extends RadioButton
猜你喜欢
  • 2013-05-07
  • 2016-09-06
  • 1970-01-01
  • 1970-01-01
  • 2022-10-18
  • 1970-01-01
  • 1970-01-01
  • 2019-08-07
  • 1970-01-01
相关资源
最近更新 更多