2016-05-11 13 views
0

AndroidのRadioButtonウィジェットのカスタマイズに問題があります。 アイコンの下に1行のテキストを含むRadioButtonが必要です。 iOSのボトムバーのようなアスペクト。アイコンとテキストは別の色でチェックされています。 私はRadioButtonを拡張し、レイアウトを膨張させるカスタムクラスを作成しました。カスタムレイアウトRadioButton Android

ウィジェットはうまく動作しますが、私のレイアウトは表示されません。それは、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" /> 

+0

をプロパティを設定します。あなたが右にチェックしたときにテキストの色を変更したい –

+0

屋。私はあなた自身のradiobuttonViewを作成する代わりにhttp://stackoverflow.com/questions/19163628/adding-custom-radio-buttons-in-androidのようなものを使う方が良いと思います。カスタマイズする以外に何かしたいことがない限りラジオボタン。 –

答えて

0

単なるテキストセレクタ

<?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" 
関連する問題