Android开发-CheckedTextView复选框自定义样式-AndroidStudio
2017-04-11 15:27
281 查看
转载请注明出处:http://blog.csdn.net/iwanghang/article/details/70053816
觉得博文有用,请点赞,请评论,请关注,谢谢!~
百度了一大圈,其实也没有好的方法,就是selector的练习~~
老规矩,先上GIF动态图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:
res\layout\my_select_dialog_multichoice.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawableLeft="@drawable/checkbox_style"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="?android:attr/textColorAlertDialogListItem"
android:gravity="center_vertical"
android:ellipsize="marquee">
</CheckedTextView>
<!--文字居中-->
<!--android:textAlignment="center"-->
<!--复选框 左显示-->
<!--android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"-->
<!--复选框右显示(默认位置)-->
<!--android:checkMark="?android:attr/listChoiceIndicatorMultiple"-->
<!--自定义的复选框样式 左显示-->
<!--android:drawableLeft="@drawable/checkbox_style"-->
<!--自定义的复选框样式 (默认位置)-->
<!--android:checkMark="@drawable/checkbox_style"-->res\drawable\checkbox_style.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/check_nnn" android:state_checked="false"></item>
<item android:drawable="@drawable/check_nnn" android:state_selected="true"></item>
<item android:drawable="@drawable/check_nnn" android:state_pressed="true"></item>
<item android:drawable="@drawable/check_yyy"></item>
</selector>MainActivity.java:
package com.iwanghang.listviewdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private ListView lv;
private ArrayAdapter<String> adapter;
//private ArrayAdapter<ListData> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 经典的adndroid.R的item
//adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
// 多选item
//adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_multichoice);
// 自己写的item 使用CheckedTextView
adapter = new ArrayAdapter<String>(this,R.layout.my_select_dialog_multichoice);
// 单选item (也可以当多选用)
//adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice);
// 勾选item
//adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked);
// 小字体item
//adapter = new ArrayAdapter<String>(this,android.R.layout.simple_gallery_item);
//adapter = new ArrayAdapter<String>(this,R.layout.text_item);
//adapter = new ArrayAdapter<ListData>(this,R.layout.text_item);
lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(adapter);
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
//adapter.add(new ListData("android","man",404));
//adapter.add(new ListData("android","woman",388));
//adapter.add(new ListData("android","null",888));
// 其中 多选、单选、勾选 可以用下面2个函数还设置 多选/单选 属性
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // 多选
//lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); // 单选
// checkedTextView = (CheckedTextView) findViewById(R.layout.my_select_dialog_multichoice);
// //checkedTextView.setCheckMarkDrawable(android.R.drawable.arrow_down_float);
//
// //根据数组id得到数组类型
// TypedArray ta = getBaseContext().getTheme().obtainStyledAttributes(new int[]{R.drawable.icon_box_empty});
// //初始化绘制目标
// Drawable indicator = ta.getDrawable(0);
// checkedTextView = new CheckedTextView(this);
// checkedTextView.setText("test1");
// //得到绘制目标后放入选择框中
// checkedTextView.setCheckMarkDrawable(indicator);
// item点击监听
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 写法1:
//String name = adapter.getItem(position).getUserName();
//String sex = adapter.getItem(position).getSex();
//int age = adapter.getItem(position).getAge();
//Toast.makeText(MainActivity.this, "你点击了:"+name+" "+sex+" "+age, Toast.LENGTH_SHORT).show();
// 写法2:
//ListData ld = adapter.getItem(position);
//Toast.makeText(MainActivity.this, String.format("%s %s %s",ld.getUserName(),ld.getSex(),ld.getAge()), Toast.LENGTH_SHORT).show();
}
});
}
}
res\layout\activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.iwanghang.listviewdemo.MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#000000"
android:text="CheckedTextViewDemo" />
<ListView
android:layout_below="@id/tv"
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</RelativeLayout>
转载请注明出处:http://blog.csdn.net/iwanghang/article/details/70053816
欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com
觉得博文有用,请点赞,请评论,请关注,谢谢!~
觉得博文有用,请点赞,请评论,请关注,谢谢!~
百度了一大圈,其实也没有好的方法,就是selector的练习~~
老规矩,先上GIF动态图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:
res\layout\my_select_dialog_multichoice.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawableLeft="@drawable/checkbox_style"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="?android:attr/textColorAlertDialogListItem"
android:gravity="center_vertical"
android:ellipsize="marquee">
</CheckedTextView>
<!--文字居中-->
<!--android:textAlignment="center"-->
<!--复选框 左显示-->
<!--android:drawableLeft="?android:attr/listChoiceIndicatorMultiple"-->
<!--复选框右显示(默认位置)-->
<!--android:checkMark="?android:attr/listChoiceIndicatorMultiple"-->
<!--自定义的复选框样式 左显示-->
<!--android:drawableLeft="@drawable/checkbox_style"-->
<!--自定义的复选框样式 (默认位置)-->
<!--android:checkMark="@drawable/checkbox_style"-->res\drawable\checkbox_style.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/check_nnn" android:state_checked="false"></item>
<item android:drawable="@drawable/check_nnn" android:state_selected="true"></item>
<item android:drawable="@drawable/check_nnn" android:state_pressed="true"></item>
<item android:drawable="@drawable/check_yyy"></item>
</selector>MainActivity.java:
package com.iwanghang.listviewdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
private ListView lv;
private ArrayAdapter<String> adapter;
//private ArrayAdapter<ListData> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 经典的adndroid.R的item
//adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1);
// 多选item
//adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_multichoice);
// 自己写的item 使用CheckedTextView
adapter = new ArrayAdapter<String>(this,R.layout.my_select_dialog_multichoice);
// 单选item (也可以当多选用)
//adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice);
// 勾选item
//adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked);
// 小字体item
//adapter = new ArrayAdapter<String>(this,android.R.layout.simple_gallery_item);
//adapter = new ArrayAdapter<String>(this,R.layout.text_item);
//adapter = new ArrayAdapter<ListData>(this,R.layout.text_item);
lv = (ListView) findViewById(R.id.lv);
lv.setAdapter(adapter);
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
adapter.add("hello");
//adapter.add(new ListData("android","man",404));
//adapter.add(new ListData("android","woman",388));
//adapter.add(new ListData("android","null",888));
// 其中 多选、单选、勾选 可以用下面2个函数还设置 多选/单选 属性
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); // 多选
//lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); // 单选
// checkedTextView = (CheckedTextView) findViewById(R.layout.my_select_dialog_multichoice);
// //checkedTextView.setCheckMarkDrawable(android.R.drawable.arrow_down_float);
//
// //根据数组id得到数组类型
// TypedArray ta = getBaseContext().getTheme().obtainStyledAttributes(new int[]{R.drawable.icon_box_empty});
// //初始化绘制目标
// Drawable indicator = ta.getDrawable(0);
// checkedTextView = new CheckedTextView(this);
// checkedTextView.setText("test1");
// //得到绘制目标后放入选择框中
// checkedTextView.setCheckMarkDrawable(indicator);
// item点击监听
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// 写法1:
//String name = adapter.getItem(position).getUserName();
//String sex = adapter.getItem(position).getSex();
//int age = adapter.getItem(position).getAge();
//Toast.makeText(MainActivity.this, "你点击了:"+name+" "+sex+" "+age, Toast.LENGTH_SHORT).show();
// 写法2:
//ListData ld = adapter.getItem(position);
//Toast.makeText(MainActivity.this, String.format("%s %s %s",ld.getUserName(),ld.getSex(),ld.getAge()), Toast.LENGTH_SHORT).show();
}
});
}
}
res\layout\activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.iwanghang.listviewdemo.MainActivity">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textColor="#000000"
android:text="CheckedTextViewDemo" />
<ListView
android:layout_below="@id/tv"
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</RelativeLayout>
转载请注明出处:http://blog.csdn.net/iwanghang/article/details/70053816
欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com
觉得博文有用,请点赞,请评论,请关注,谢谢!~
相关文章推荐
- Android开发-TextView background 背景 颜色 圆角-AndroidStudio
- 50个Android开发技巧(10 为TextView添加样式)
- Android开发-TextView background 背景 颜色 圆角-AndroidStudio
- 50个Android开发技巧(10 为TextView添加样式)
- 50个Android开发技巧(10 为TextView添加样式)
- Android开发-TextViewDemo-AndroidStudio
- Android开发-TextViewDemo-AndroidStudio
- Android开发-CheckedTextView复选框居左文字居中-AndroidStudio
- 50个Android开发技巧(10 为TextView加入样式)
- Android的TextView/EditText使用CharacterStyle&SpannableString来处理图片显示、字体样式、超链接等
- Android中ListView中使用CheckedTextView和CheckBox的理解
- Android开发技巧:在TextView控件中单击链接弹出Activity
- Android开发教程 --- 葵花宝典第五层 控件之 Menu DatePicker TimePicker AutoCompleteTextView MultiAutoCompleteTextView
- Android控件开发之TextView
- Android软件开发之TextView详解(六)
- [Android开发] 代码code设置9.png/9-patch 图片背景后,此view中的TextView等控件显示不正常(常见于listview中)
- 【Android游戏开发之七】(游戏开发中需要的样式)再次剖析游戏开发中对SurfaceView中添加组件方案!
- Android应用开发——TextView控件属性列表
- Android的TextView/EditText使用CharacterStyle&SpannableString来处理图片显示、字体样式、超链接等
- Android软件开发之TextView详解(四)