<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.test.custom.MainActivity"> <com.test.custom.TitleView android:id="@+id/myTitleView" android:layout_width="match_parent" android:layout_height="60dp" app:centerText="自定义标题栏" app:centerTextColor="#ffffff" app:centerTextSize="20sp" app:center_BG_Color="@color/colorAccent" app:leftText="返回" app:leftTextColor="#000000" app:leftTextSize="20sp" app:left_BG_Color="@drawable/bg_color" app:rightText="确定" app:rightTextColor="#000000" app:rightTextSize="20sp" app:right_GB_Color="@drawable/bg_color"> </com.test.custom.TitleView></LinearLayout>
//自定义View应用的布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/left_button" android:layout_width="100dp" android:layout_height="50dp" android:gravity="center" /> <TextView android:id="@+id/center_button" android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:gravity="center" /> <TextView android:id="@+id/right_button" android:layout_width="100dp" android:layout_height="50dp" android:gravity="center" /> </LinearLayout>//attrs中自定义的属性<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="TitleView"> <attr name="leftText" format="string"></attr> <attr name="left_BG_Color" format="reference"></attr>//设置Drawable状态选择器 <attr name="leftTextSize" format="dimension"></attr> <attr name="leftTextColor" format="color"></attr> <attr name="centerText" format="string"></attr> <attr name="center_BG_Color" format="color"></attr> <attr name="centerTextSize" format="dimension"></attr> <attr name="centerTextColor" format="color"></attr> <attr name="rightText" format="string"></attr> <attr name="right_GB_Color" format="reference"></attr>//设置Drawable状态选择器 <attr name="rightTextSize" format="dimension"></attr> <attr name="rightTextColor" format="color"></attr> </declare-styleable> </resources>public class MainActivity extends AppCompatActivity { private TitleView myTitleView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myTitleView = (TitleView) findViewById(R.id.myTitleView); myTitleView.setLeftClick(new TitleView.LeftClick() { @Override public void onClick(View v, String name) { Toast.makeText(MainActivity.this, name + "点击了", Toast.LENGTH_SHORT).show(); } }); myTitleView.setRighttClick(new TitleView.RighttClick() { @Override public void onClick(View v, String name) { Toast.makeText(MainActivity.this, name + "点击了", Toast.LENGTH_SHORT).show(); } }); } }//自定义View页面public class TitleView extends LinearLayout { private TextView left_button; private TextView center_button; private TextView right_button; public TitleView(Context context) { super(context); initView(context, null); } public TitleView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); initView(context, attrs); } private void initView(Context context, @Nullable AttributeSet attrs) { if (attrs == null) { return; } //自定义view使用布局,这里必须是this,不然不显示 View view = LayoutInflater.from(context).inflate(R.layout.mytitle, this); left_button = (TextView) view.findViewById(R.id.left_button); center_button = (TextView) view.findViewById(R.id.center_button); right_button = (TextView) view.findViewById(R.id.right_button); //查找属性,并设置 initAttrs(attrs); left_button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { leftClick.onClick(v, left_button.getText().toString()); } }); right_button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { rightClick.onClick(v, right_button.getText().toString()); } }); } 自定义的接口,点击左右两个按钮弹出吐司, private LeftClick leftClick; public interface LeftClick { void onClick(View v, String name); } public void setLeftClick(LeftClick leftClick) { this.leftClick = leftClick; } private RighttClick rightClick; public interface RighttClick { void onClick(View v, String name); } public void setRighttClick(RighttClick rightClick) { this.rightClick = rightClick; } private void initAttrs(@Nullable AttributeSet attrs) { TypedArray typedArray = getResources().obtainAttributes(attrs, R.styleable.TitleView); int leftTextColor = typedArray.getColor(R.styleable.TitleView_leftTextColor, Color.RED); float leftTextSize = typedArray.getDimension(R.styleable.TitleView_leftTextSize, 10); String leftText = typedArray.getString(R.styleable.TitleView_leftText); Drawable left_BG_Color = typedArray.getDrawable(R.styleable.TitleView_left_BG_Color); left_button.setTextColor(leftTextColor); left_button.setTextSize(leftTextSize); left_button.setText(leftText); left_button.setBackgroundDrawable(left_BG_Color); int centerTextColor = typedArray.getColor(R.styleable.TitleView_centerTextColor, Color.RED); float centerTextSize = typedArray.getDimension(R.styleable.TitleView_centerTextSize, 10); String centerText = typedArray.getString(R.styleable.TitleView_centerText); int center_BG_Color = typedArray.getColor(R.styleable.TitleView_center_BG_Color, Color.RED); center_button.setTextColor(centerTextColor); center_button.setTextSize(centerTextSize); center_button.setText(centerText); center_button.setBackgroundColor(center_BG_Color); int rightTextColor = typedArray.getColor(R.styleable.TitleView_rightTextColor, Color.RED); float rightTextSize = typedArray.getDimension(R.styleable.TitleView_rightTextSize, 10); String rightText = typedArray.getString(R.styleable.TitleView_rightText); Drawable right_GB_Color = typedArray.getDrawable(R.styleable.TitleView_right_GB_Color); right_button.setTextColor(rightTextColor); right_button.setTextSize(rightTextSize); right_button.setText(rightText); right_button.setBackgroundDrawable(right_GB_Color); } }
自定义标题栏(左中右三块)
最新推荐文章于 2021-05-26 04:31:09 发布