Value/attrs.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#00ff00" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="50dp"> <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="我是内容,请选择" android:textSize="20sp"/> <CheckBox android:id="@+id/cb_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:background="#00ff00" android:clickable="false" android:focusable="false" /> </RelativeLayout> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="50dp" android:background="#00ff00" > <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="我是内容,请选择" android:textSize="20sp"/> <CheckBox android:id="@+id/cb_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:background="#00ff00" android:clickable="false" android:focusable="false" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="50dp" <span style="white-space:pre"> </span>//注意这里!!!!!!!!!!!!!!!!!<span style="font-family: Arial, Helvetica, sans-serif;"> </span> android:background="#00ff00" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> // 注意这里!!!!!!!!!!!!!!!!!!!! <TextView android:id="@+id/tv_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="我是内容,请选择" android:textSize="20sp"/> <CheckBox android:id="@+id/cb_checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:background="#00ff00" android:clickable="false" android:focusable="false" /> </RelativeLayout> </RelativeLayout>
SettingCheckView.java
public class SettingCheckView extends LinearLayout{ private TextView tv_content; private CheckBox cb_status; public SettingCheckView(Context context, AttributeSet attrs) { super(context, attrs); initView(context);
<span style="white-space:pre"> </span>//这个<span style="font-family: Arial, Helvetica, sans-serif;">http://schemas.android.com/com.weixin.justtext1104要跟布局里的一样</span> String bigtitle = attrs.getAttributeValue("http://schemas.android.com/com.weixin.justtext1104","bigtitle"); //从布局中获取文本,写给自己的ui tv_content.setText(bigtitle); } private void initView(Context context) { addView(View.inflate(context, R.layout.setting_check_view, null)); tv_content = (TextView) findViewById(R.id.tv_content); cb_status = (CheckBox) findViewById(R.id.cb_checkbox); } /** * 让文本选中 * */ public void setChecked(boolean state){ cb_status.setChecked(state); } /** * 判断是否选中 * */ public boolean isChecked(){ return cb_status.isChecked(); } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
//注意这个名字要attr.xml中的要一样(命名空间一般用包名就行) xmlns:SettingCheckView="http://schemas.android.com/com.weixin.justtext1104" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.weixin.justtext1104.SettingCheckView android:id="@+id/scv" android:layout_width="fill_parent" android:layout_height="wrap_content" SettingCheckView:bigtitle = "我是一个选项请勾选" > </com.weixin.justtext1104.SettingCheckView> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="SettingCheckView"> <attr name="bigtitle" format="string" /> </declare-styleable> </resources>