<!--?xml version=1.0 encoding=utf-8?--> <relativelayout android:background="@drawable/selector_blue" android:id="@+id/rl_show_address" android:layout_height="60dip" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <textview android:id="@+id/tv_title" android:layout_height="wrap_content" android:layout_marginleft="5dip" android:layout_margintop="1dip" android:layout_width="wrap_content" android:text="这是标题" android:textcolor="#000000" android:textsize="20sp"> <textview android:id="@+id/tv_desc" android:layout_below="@id/tv_title" android:layout_height="wrap_content" android:layout_marginleft="6dip" android:layout_margintop="1dip" android:layout_width="wrap_content" android:text="这是描述内容" android:textcolor="#99ff0000" android:textsize="14sp"> <checkbox android:clickable="false" android:focusable="false" android:id="@+id/cb_status" android:layout_alignparentright="true" android:layout_centervertical="true" android:layout_height="wrap_content" android:layout_width="wrap_content"> <!-- 加一条分割线 --> <view android:background="#000000/" android:layout_alignbottom="@id/cb_status" android:layout_alignparentbottom="true" android:layout_height="0.2dip" android:layout_margintop="7dip" android:layout_width="match_parent"> </view></checkbox></textview></textview></relativelayout>
<!--?xml version=1.0 encoding=utf-8?-->
<resources>
<declare-styleable name="combinationView">
</attr>
</attr>
</attr>
</declare-styleable>
</resources>
package com.example.combinationview;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class CombinationView extends RelativeLayout {
private TextView tv_title;
private TextView tv_desc;
private CheckBox cb_status;
// 命名空间,在引用这个自定义组件的时候,需要用到
private String namespace = http://schemas.android.com/apk/res/com.example.combinationview;
// 标题
private String title;
// 被选中的描述
private String desc_on;
// 未被选中的描述
private String desc_off;
public CombinationView(Context context, AttributeSet attrs) {
super(context, attrs);
// 将自定义组合控件的布局渲染成View
View view = View.inflate(context, R.layout.layout_combinationview, this);
tv_title = (TextView) view.findViewById(R.id.tv_title);
tv_desc = (TextView) view.findViewById(R.id.tv_desc);
cb_status = (CheckBox) view.findViewById(R.id.cb_status);
title = attrs.getAttributeValue(namespace, title);
desc_on = attrs.getAttributeValue(namespace, desc_on);
desc_off = attrs.getAttributeValue(namespace, desc_off);
System.out.println(title + : + desc_on + : + desc_off);
// 初始化到子控件
if (title != null) {
tv_title.setText(title);
}
if (desc_off != null) {
tv_desc.setText(desc_off);
}
}
/**
* 判断是否被选中
*
* @return
*/
public boolean isChecked() {
return cb_status.isChecked();
}
/**
* 设置选中的状态
*
* @param isChecked
*/
public void setChecked(boolean isChecked) {
cb_status.setChecked(isChecked);
if (isChecked) {
tv_desc.setText(desc_on);
} else {
tv_desc.setText(desc_off);
}
}
}
<linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:example="http://schemas.android.com/apk/res/com.example.combinationview"> <com.example.combinationview.combinationview android:id="@+id/cv_first" android:layout_height="wrap_content" android:layout_width="match_parent" example:desc_off="我是未被选中的描述1" example:desc_on="我是被选中的描述1" example:title="我是标题1"> </com.example.combinationview.combinationview> <com.example.combinationview.combinationview android:id="@+id/cv_second" android:layout_height="wrap_content" android:layout_width="match_parent" example:desc_off="我是未被选中的描述2" example:desc_on="我是被选中的描述2" example:title="我是标题2"> </com.example.combinationview.combinationview> <com.example.combinationview.combinationview android:id="@+id/cv_third" android:layout_height="wrap_content" android:layout_width="match_parent" example:desc_off="我是未被选中的描述3" example:desc_on="我是被选中的描述3" example:title="我是标题3"> </com.example.combinationview.combinationview> <com.example.combinationview.combinationview android:id="@+id/cv_fourth" android:layout_height="wrap_content" android:layout_width="match_parent" example:desc_off="我是未被选中的描述4" example:desc_on="我是被选中的描述4" example:title="我是标题4"> </com.example.combinationview.combinationview> </linearlayout>
xmlns:example=http://schemas.android.com/apk/res/com.example.combinationview
package com.example.combinationview;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.Activity;
public class MainActivity extends Activity implements OnClickListener {
private CombinationView cv_first;
private CombinationView cv_second;
private CombinationView cv_third;
private CombinationView cv_fourth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cv_first = (CombinationView) findViewById(R.id.cv_first);
cv_second = (CombinationView) findViewById(R.id.cv_second);
cv_third = (CombinationView) findViewById(R.id.cv_third);
cv_fourth = (CombinationView) findViewById(R.id.cv_fourth);
cv_first.setOnClickListener(this);
cv_second.setOnClickListener(this);
cv_third.setOnClickListener(this);
cv_fourth.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.cv_first:
if (cv_first.isChecked()) {
cv_first.setChecked(false);
} else {
cv_first.setChecked(true);
}
break;
case R.id.cv_second:
if (cv_second.isChecked()) {
cv_second.setChecked(false);
} else {
cv_second.setChecked(true);
}
break;
case R.id.cv_third:
if (cv_third.isChecked()) {
cv_third.setChecked(false);
} else {
cv_third.setChecked(true);
}
break;
case R.id.cv_fourth:
if (cv_fourth.isChecked()) {
cv_fourth.setChecked(false);
} else {
cv_fourth.setChecked(true);
}
break;
default:
break;
}
}
}
package com.xiong.demo1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
TitleBarView titleBarView = (TitleBarView) findViewById(R.id.tbar_test);
titleBarView.getTextViewRigth().setVisibility(View.GONE);
titleBarView.setTitleBarChangerLiseter(new ItitleOnChangeLister() {
@Override
public void setLeftOnClickLister() {
finish();
}
@Override
public void setRigthOnClickLister() {
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xionglh="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.xiong.demo1.TitleBarView
android:id="@+id/tbar_test"
android:layout_width="match_parent"
android:layout_height="45dp"
xionglh:titleBar_center_text="首页"
xionglh:titleBar_center_textColor="@android:color/black"
xionglh:titleBar_center_text_size="18sp"
xionglh:titleBar_left_bg="@mipmap/left_back"
xionglh:titleBar_right_text="安全中心"
xionglh:titleBar_right_text_size="12sp"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="TitleBar">
<attr name="titleBar_center_text_size" format="dimension"/>
<attr name="titleBar_center_text" format="string"/>
<attr name="titleBar_center_textColor" format="color"/>
<attr name="titleBar_left_bg" format="reference"/>
<attr name="titleBar_right_text_size" format="dimension"/>
<attr name="titleBar_right_text" format="string"/>
<attr name="titleBar_right_textColor" format="color"/>
</declare-styleable>
</resources>
package com.xiong.demo1;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class TitleBarView extends RelativeLayout {
private ItitleOnChangeLister mItitleOnChangeLister;
private ImageView mImgLeft;
private TextView mTxtCenter, mTxtRigth;
private float mTitleCenterTextSize;//标题字体大小
private String mTitleCenterText;//标题文字
private int mTitleCenterTextColor;//标题颜色
private int mLeftBg;//左边返回按钮
private float mTitleRigthTextSize;//标题字体大小
private String mTitleRigthText;//标题文字
private int mTitleRigthColor;//标题颜色
public TitleBarView(Context context, AttributeSet attrs) {
super(context, attrs);
int defualtSize = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics());
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.TitleBar);
mTitleCenterTextSize = typedArray.getDimension(R.styleable.TitleBar_titleBar_center_text_size, defualtSize);
mTitleCenterText = typedArray.getString(R.styleable.TitleBar_titleBar_center_text);
mTitleCenterTextColor = typedArray.getColor(R.styleable.TitleBar_titleBar_center_textColor, Color.RED);
mLeftBg = typedArray.getResourceId(R.styleable.TitleBar_titleBar_left_bg, R.mipmap.left_back);
mTitleRigthTextSize = typedArray.getDimension(R.styleable.TitleBar_titleBar_right_text_size, defualtSize);
mTitleRigthText = typedArray.getString(R.styleable.TitleBar_titleBar_right_text);
mTitleRigthColor = typedArray.getColor(R.styleable.TitleBar_titleBar_right_textColor, Color.RED);
typedArray.recycle();
initView();
}
private void initView() {
mTxtCenter = new TextView(getContext());
mTxtCenter.setText(mTitleCenterText);
mTxtCenter.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleCenterTextSize);
mTxtCenter.setTextColor(mTitleCenterTextColor);
LayoutParams centerParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
centerParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);
addView(mTxtCenter, centerParams);
mTxtRigth = new TextView(getContext());
mTxtRigth.setText(mTitleRigthText);
mTxtRigth.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTitleRigthTextSize);
mTxtRigth.setTextColor(mTitleRigthColor);
LayoutParams rigthParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rigthParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);
rigthParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);
addView(mTxtRigth, rigthParams);
mImgLeft = new ImageView(getContext());
mImgLeft.setImageResource(mLeftBg);
LayoutParams leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
leftParams.setMargins(6, 0, 0, 0);
leftParams.addRule(RelativeLayout.CENTER_VERTICAL, TRUE);
addView(mImgLeft, leftParams);
View view = new View(getContext());
view.setMinimumWidth(1);
view.setBackgroundColor(getResources().getColor(R.color.gray_767676));
LayoutParams viewParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 1);
viewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
addView(view, viewParams);
mImgLeft.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mItitleOnChangeLister.setLeftOnClickLister();
}
});
mTxtRigth.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mItitleOnChangeLister.setRigthOnClickLister();
}
});
}
public void setTitleBarChangerLiseter(ItitleOnChangeLister ititleOnChangeLister) {
this.mItitleOnChangeLister = ititleOnChangeLister;
}
public ImageView getLeftImage() {
return mImgLeft;
}
public TextView getTextViewCenter() {
return mTxtCenter;
}
public TextView getTextViewRigth() {
return mTxtRigth;
}
}
package com.xiong.demo1;
public interface ItitleOnChangeLister {
void setLeftOnClickLister();
void setRigthOnClickLister();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有