自定义土司参考了源码中土司的定义。
之前实现了CheckBox的自定义事件,这里只要基于他就可以了。
首先是setting_click_view.xml的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp" >
<TextView
android:id="@+id/tv_title"
android:text="我是标题"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="5dip"
android:textColor="#99000000"
android:textSize="20sp" />
<TextView
android:text="我是描述"
android:layout_below="@id/tv_title"
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="2dip"
android:textColor="#66000000"
android:textSize="16sp" />
<ImageView
android:src="@drawable/jiantou1_pressed"
android:clickable="false"
android:focusable="false"
android:id="@+id/cb_status"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5dip"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dip"
android:layout_marginRight="5dp"
android:layout_marginTop="8dp"
android:background="#88000000"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
接下来是SettingClickView:
package com.ustc.mobilemanager.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ustc.mobilemanager.R;
/**
*
* 自定义组合控件(两个TextView 一个ImageView)
*
* @author
*
*/
public class SettingClickView extends RelativeLayout {
private TextView tv_desc;
private TextView tv_title;
private String desc_on;
private String desc_off;
private void initView(Context context) {
// 把一个布局文件转化为----》View并且加载在SettingItemView类中
View view = View.inflate(context, R.layout.setting_click_view,
SettingClickView.this);
tv_desc = (TextView) this.findViewById(R.id.tv_desc);
tv_title = (TextView) this.findViewById(R.id.tv_title);
}
public SettingClickView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
/**
*
* 带有两个参数的构造方法,布局文件使用的时候调用
*
* @param context
* @param attrs
*/
public SettingClickView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
// String value = attrs.getAttributeValue(0);
// System.out.println(value);
// String value1 = attrs.getAttributeValue(1);
// System.out.println(value1);
// String value2 = attrs.getAttributeValue(2);
// System.out.println(value2);
// String value3 = attrs.getAttributeValue(3);
// System.out.println(value3);
// String value4 = attrs.getAttributeValue(4);
// System.out.println(value4);
// String value5 = attrs.getAttributeValue(5);
// System.out.println(value5);
String title = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/com.ustc.mobilemanager",
"title");
desc_on = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/com.ustc.mobilemanager",
"desc_on");
desc_off = attrs.getAttributeValue(
"http://schemas.android.com/apk/res/com.ustc.mobilemanager",
"desc_off");
tv_title.setText(title);
setDesc(desc_off);
}
public SettingClickView(Context context) {
super(context);
initView(context);
}
/**
*
*
* 设置组合控件的状态
*/
public void setChecked(boolean checked) {
if (checked) {
setDesc(desc_on);
} else {
setDesc(desc_off);
}
}
/**
* 组合控件的描述信息
*
*/
public void setDesc(String text) {
tv_desc.setText(text);
}
/**
* 组合控件的标题
*
*/
public void setTitle(String title) {
tv_title.setText(title);
}
}
这时候在activity_setting.xml加入刚刚自定义的布局:
<com.ustc.mobilemanager.ui.SettingClickView
android:id="@+id/scv_changebg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
SettingActivity中设置点击事件,并使用SharedPrefrences保存选中的状态:
scv_changebg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int dd = sp.getInt("which", 0);
// 弹出一个对话框
AlertDialog.Builder builder = new Builder(SettingActivity.this);
builder.setTitle("归属地提示框风格");
builder.setSingleChoiceItems(items, dd,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// 保存选择参数
Editor editor = sp.edit();
editor.putInt("which", which);
editor.commit();
scv_changebg.setDesc(items[which]);
// 取消对话框
dialog.dismiss();
}
});
builder.setNegativeButton("cancel", null);
builder.show();
}
});
我们在自定义土司布局中,可以取出刚刚保存的状态。
先看看自定义土司的布局:address_show.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_call" />
<TextView
android:textColor="#000000"
android:id="@+id/tv_address"
android:textSize="22sp"
android:text="号码归属地"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
/**
*
* 自定义土司
*
* @param address
*/
public void MyToast(String address) {
view = View.inflate(this, R.layout.address_show, null);
TextView textview = (TextView) view.findViewById(R.id.tv_address);
// "半透明","活力橙","卫士蓝","金属灰","苹果绿"
int[] ids = { R.drawable.call_locate_white,
R.drawable.call_locate_orange, R.drawable.call_locate_blue,
R.drawable.call_locate_gray, R.drawable.call_locate_green };
SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
view.setBackgroundResource(ids[sp.getInt("which", 0)]);
textview.setText(address);
// 窗体的参数就设置好了
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
params.format = PixelFormat.TRANSLUCENT;
params.type = WindowManager.LayoutParams.TYPE_TOAST;
wm.addView(view, params);
}
运行: