自定义viewgroup控件-麦片
来源:互联网 发布:淘宝买家秀怎么发视频 编辑:程序博客网 时间:2024/04/27 14:43
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class CustomTitleBar extends RelativeLayout {
private Button titleBarLeftBtn;
private Button titleBarRightBtn;
private TextView titleBarTitle;
public CustomTitleBar(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.custom_title_bar, this, true);
// View.inflate(context, R.layout.custom_title_bar, this);
titleBarLeftBtn = (Button) findViewById(R.id.title_bar_left);
titleBarRightBtn = (Button) findViewById(R.id.title_bar_right);
titleBarTitle = (TextView) findViewById(R.id.title_bar_title);
titleBarTitle.setText("32434234234");
// TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CustomTitleBar);
// if (attributes != null) {
// //先处理左边按钮
// //获取是否要显示左边按钮
// boolean leftButtonVisible = attributes.getBoolean(R.styleable.CustomTitleBar_left_button_visible, true);
// if (leftButtonVisible) {
// titleBarLeftBtn.setVisibility(View.VISIBLE);
// } else {
// titleBarLeftBtn.setVisibility(View.INVISIBLE);
// }
// //设置左边按钮的文字
// String leftButtonText = attributes.getString(R.styleable.CustomTitleBar_left_button_text);
// if (!TextUtils.isEmpty(leftButtonText)) {
// titleBarLeftBtn.setText(leftButtonText);
// } else {
// //设置左边图片icon 这里是二选一 要么只能是文字 要么只能是图片
// int leftButtonDrawable = attributes.getResourceId(R.styleable.CustomTitleBar_left_button_drawable, R.mipmap.ic_launcher);
// if (leftButtonDrawable != -1) {
// titleBarLeftBtn.setBackgroundResource(leftButtonDrawable);
// }
// }
//
// //处理标题
// //先获取标题是否要显示图片icon
// int titleTextDrawable = attributes.getResourceId(R.styleable.CustomTitleBar_title_text_drawable, -1);
// if (titleTextDrawable != -1) {
// titleBarTitle.setBackgroundResource(titleTextDrawable);
// } else {
// //如果不是图片标题 则获取文字标题
// String titleText = attributes.getString(R.styleable.CustomTitleBar_title_text);
// if (!TextUtils.isEmpty(titleText)) {
// titleBarTitle.setText(titleText);
// }
// }
//
// //先处理右边按钮
// //获取是否要显示右边按钮
// boolean rightButtonVisible = attributes.getBoolean(R.styleable.CustomTitleBar_right_button_visible, true);
// if (rightButtonVisible) {
// titleBarRightBtn.setVisibility(View.VISIBLE);
// } else {
// titleBarRightBtn.setVisibility(View.INVISIBLE);
// }
// //设置右边按钮的文字
// String rightButtonText = attributes.getString(R.styleable.CustomTitleBar_right_button_text);
// if (!TextUtils.isEmpty(rightButtonText)) {
// titleBarRightBtn.setText(rightButtonText);
// //设置右边按钮文字颜色
// } else {
// //设置右边图片icon 这里是二选一 要么只能是文字 要么只能是图片
// int rightButtonDrawable = attributes.getResourceId(R.styleable.CustomTitleBar_right_button_drawable, -1);
// if (rightButtonDrawable != -1) {
// titleBarRightBtn.setBackgroundResource(rightButtonDrawable);
// }
// }
// attributes.recycle();
// }
}
public void setTitleClickListener(OnClickListener onClickListener) {
if (onClickListener != null) {
titleBarLeftBtn.setOnClickListener(onClickListener);
titleBarRightBtn.setOnClickListener(onClickListener);
}
}
public Button getTitleBarLeftBtn() {
return titleBarLeftBtn;
}
public Button getTitleBarRightBtn() {
return titleBarRightBtn;
}
public TextView getTitleBarTitle() {
return titleBarTitle;
}
}
//布局
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="@+id/title_bar_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:background="@null"
android:minHeight="45dp"
android:minWidth="45dp"
android:textSize="14sp" />
<TextView
android:id="@+id/title_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:singleLine="true"
android:textSize="17sp" />
<Button
android:id="@+id/title_bar_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="7dp"
android:background="@null"
android:minHeight="45dp"
android:minWidth="45dp"
android:textSize="14sp" />
</merge>
布局
<com.example.customtitlebar.CustomTitleBar
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="10dp"
lee:left_button_text="左边"
lee:right_button_drawable="@mipmap/ic_launcher"
lee:title_text="标题6"
lee:title_text_color="#000" />
- 自定义viewgroup控件-麦片
- 自定义控件ViewGroup
- 自定义控件ViewGroup实战
- 自定义控件--ViewGroup篇
- Android 自定义ViewGroup控件
- 自定义组合控件ViewGroup
- ViewGroup-自定义控件
- Android自定义控件 -- 自定义ViewGroup
- androdi 自定义控件 ViewGroup(一)
- 自定义控件(View viewGroup)
- 【自定义控件】自定义ViewGroup 在ViewGroup中显示TextView
- 自定义控件基础知识2 -- 自定义ViewGroup
- 自定义view (一)自定义控件 viewGroup
- Android控件:自定义View和ViewGroup相关
- android控件架构、自定义View、ViewGroup
- Android自定义控件 沉浸式ViewGroup
- Androd自定义控件(四)自定义类继承viewgroup
- Android自定义控件-完全自定义(继承View ,ViewGroup)
- 图片二次采样使用AsyncTask
- /../libstdc++.so.6: versionCXXABI_1.3.8' not found (required by /usr/lib/x86_64-linux-gnu/libicuuc.s
- Java四舍五入保留任意位数小数
- android 上传图片到webservice
- Java多线程编程-(14)-Java中的队列同步器AQS和ReentrantLock锁原理简要分析
- 自定义viewgroup控件-麦片
- imageview+handler+gridview+上拉下拉.xml
- 指针和数组
- 【深度相机系列一】iPhone X的原深感相机到底是个什么玩意?
- [漫画游记] 一张图秒懂SDN的12大误区
- Executor框架
- js跨域问题
- 区间内素数
- 使用OpenCV4Android时的动态权限获取