【问题标题】:Custom Layout RadioButton Android自定义布局 RadioButton Android
【发布时间】:2016-09-06 22:03:36
【问题描述】:

我在 Android 中自定义 RadioButton 小部件时遇到问题。 我需要一个带有图标和图标下单行文本的 RadioButton。 类似于 iOS 底部栏的外观,图标和文本以不同颜色选中。 我创建了一个扩展 RadioButton 的自定义类并扩展了我的布局。

小部件工作正常,但不显示我的布局,它只显示背景,不显示 imageView 和 textView。 有什么想法吗?

提前致谢

public class RadioButtonImageToggle extends RadioButton {

private Drawable imageNormalState, imageCheckedState;
private ImageView imageView;
private TextView textViewTitle;

public RadioButtonImageToggle(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initViews(context,attrs, defStyleAttr);
}

public RadioButtonImageToggle(Context context, AttributeSet attrs) {
    super(context, attrs);
    initViews(context, attrs, 0);
}


private void initViews(Context context, AttributeSet attrs, int defStyle) {


    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.RadioButtonImageToggle, defStyle, R.style.Widget_Holo_RadioImageToggle);

    try {
        // get the text and colors specified using the names in attrs.xml
        imageNormalState = a.getDrawable(R.styleable.RadioButtonImageToggle_imageNormalState);
        imageCheckedState = a.getDrawable(R.styleable.RadioButtonImageToggle_imageCheckedState);
    } finally {
        a.recycle();
    }
    LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = layoutInflater.inflate(R.layout.radio_image_toggle, null);

    imageView = (ImageView) view.findViewById(R.id.iconToggle);
    textViewTitle = (TextView) view.findViewById(R.id.textViewTitle);

    imageView.setImageDrawable(imageNormalState);
    textViewTitle.setText(getText());
}

@Override
public void toggle()
{
    if(isChecked()) {
        imageView.setImageDrawable(imageCheckedState);
        if(getParent() instanceof RadioGroup) {
           ((RadioGroup)getParent()).clearCheck();
        }
    } else {
        imageView.setImageDrawable(imageNormalState);
        setChecked(true);
    }
}

布局自定义是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:id="@+id/iconToggle"
    android:layout_gravity="center"
    android:src="@mipmap/ic_launcher" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="TITLE"
    android:id="@+id/textViewTitle"
    android:layout_gravity="center"
    android:lines="1"
    android:singleLine="true" />

【问题讨论】:

  • 我认为你不需要为它编码。您想在检查正确时更改文本的颜色
  • 是的。我认为您最好使用stackoverflow.com/questions/19163628/… 之类的东西,而不是创建自己的radiobuttonView,除非除了自定义单选按钮之外您还想做其他事情。

标签: android radio-button custom-controls


【解决方案1】:

只需为文本选择器制作可绘制文件

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#ffffffff"/>
<item android:color="@color/que_font"/></selector>

为单选按钮背景制作另一个可绘制对象

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_checked="true"><shape>
    <solid android:color="@color/select_btn_bg" />
    <corners
        android:radius="5dp"
        />

    <stroke android:width="1dp" android:color="@color/select_btn_border" />
</shape></item>

<item android:state_checked="false"><shape android:shape="rectangle">
    <!--<solid android:color="@android:color/transparent" />-->
    <solid android:color="@color/default_btn_bg" />
    <corners
        android:radius="5dp"
        />


    <stroke
        android:width="1dp"
        android:color="@color/default_btn_border"
        />

</shape></item></selector>

然后只需在您的单选按钮中设置如下属性

      android:textColor="@drawable/rbtn_text_selector"
    android:background="@drawable/rbtn_background_selector"  
android:button="@null"

【讨论】:

    猜你喜欢
    • 2013-05-07
    • 2011-05-14
    • 1970-01-01
    • 2020-02-23
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多