Android 如何将自定义布局用作RadioButton标签
我为Android 如何将自定义布局用作RadioButton标签,android,android-layout,radio-button,android-custom-view,Android,Android Layout,Radio Button,Android Custom View,我为单选按钮制作了一个自定义布局。 android类的代码如下: public class MyRadioButton extends LinearLayout implements View.OnClickListener{ private ImageView iv; private TextView tv; private RadioButton rb; private View view; public MyRadioButton(Context
单选按钮
制作了一个自定义布局。
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);
}
}
公共类MyRadioButton扩展了LinearLayout实现了View.OnClickListener{
私人影像视图四;
私家图文电视;
专用无线按钮rb;
私人视野;
公共MyRadioButton(上下文){
超级(上下文);
视图=视图.充气(上下文,R.layout.my_单选按钮,此按钮);
设置方向(水平);
rb=(RadioButton)view.findViewById(R.id.radioButton1);
tv=(TextView)view.findViewById(R.id.textView1);
iv=(ImageView)view.findViewById(R.id.imageView1);
view.setOnClickListener(这个);
rb.setOnCheckedChangeListener(null);
}
公共void setImageBitmap(位图位图){
iv.设置图像位图(位图);
}
公共视图getView(){
返回视图;
}
@凌驾
公共void onClick(视图v){
布尔值nextState=!rb.isChecked();
LinearLayout lGroup=(LinearLayout)view.getParent();
if(lGroup!=null){
int child=lGroup.getChildCount();
对于(int i=0;i我已经尝试了以下代码,它工作正常,没有错误:
<?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, AttributeSet attributes) {
这是您需要的解决方案吗?我已经尝试了以下代码,它工作正常,没有错误:
<?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, AttributeSet attributes) {
这就是您需要的解决方案吗?有两种方法可以完成这项工作:
1。当我们听到自定义视图时,它会驱使我们重写onDraw
方法,然后将我们想要的绘制到视图的画布中
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);
}
}
编辑:
我已经重写了代码,使用Glide
和Glide转换来满足圆角图像的要求
build.gradle
dependencies {
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'jp.wasabeef:glide-transformations:3.3.0'
}
有两种方法可以完成这项工作:
1。当我们听到自定义视图时,它会驱使我们重写onDraw
方法,然后将我们想要的绘制到视图的画布中
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);
}
}
编辑:
我已经重写了代码,使用Glide
和Glide转换来满足圆角图像的要求
build.gradle
dependencies {
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'jp.wasabeef:glide-transformations:3.3.0'
}
1。在创建类时,它扩展了LinearLayout或
FrameLayout,.xml布局文件(我的单选按钮内容.xml),
必须以“merge”而不是“LinearLayout”开头,否则
将在LinearLayout(您的类)中使用LinearLayout(.xml)
2.很容易创建:最终数组列表
,并传递它
对所有的孩子,如果你想,兄弟会改变每一个
其他的
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);
}
}
});
}
公共MyRadioButton(上下文){
超级(上下文);
充气(上下文,右布局,我的单选按钮,此);
}
public void SetMyViewClickable(最终数组列表){
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
对于(查看兄弟:兄弟){
((RadioButton)brother.findViewById(R.id.radioButton1)).setChecked(false);
}
}
});
}
1.创建类时,扩展LinearLayout或
FrameLayout,.xml布局文件(我的单选按钮内容.xml),
必须以“merge”而不是“LinearLayout”开头,否则
将在LinearLayout(您的类)中使用LinearLayout(.xml)
2.很容易创建:最终数组列表
,并传递它
对所有的孩子,如果你想,兄弟会改变每一个
其他的
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);
}
}
});
}
公共MyRadioButton(上下文){
超级(上下文);
充气(上下文,右布局,我的单选按钮,此);
}
public void SetMyViewClickable(最终数组列表){
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
对于(查看兄弟:兄弟){
((RadioButton)brother.findViewById(R.id.radioButton1)).setChecked(false);
}
}
});
}
有很多方法可以做到这一点,例如,您可以使用以下库
有很多方法可以做到这一点,例如,您可以使用以下库
RadioButton必须作为RadioButton的父组。您可以更具体地说,您必须在RadioButton中使用单选按钮。请使用复选框。我正在将单选按钮用于单选组中,但我必须为单选按钮自定义视图布局您必须从RadioButton扩展类,然后RadioButton必须作为RadioButton的父组,可以吗更具体地说,您必须在RadioButton组中使用单选按钮。请改用复选框。我正在将单选按钮用于单选组中,但我必须为单选按钮提供自定义视图布局。您必须从RadioButton扩展您的类,然后才可以。我需要保留问题描述中的上述布局,但公共类MyRadioButton extends LinearLayout
必须更改为公共类MyRadioButton extends RadioButton
否。我需要保留