Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to customize TopBar and attribute encapsulation

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to customize TopBar and attribute encapsulation". In daily operation, I believe many people have doubts about how to customize TopBar and attribute encapsulation. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how to customize TopBar and attribute encapsulation". Next, please follow the editor to study!

Preface

In practice, when we receive boss to do the following interface

There may be many such common interfaces. Of course, we can achieve a unified effect by writing a Topbar for each interface, but when we want to modify it, don't we have to what it? ) I'm going to write a custom basic View that can be encapsulated and used, so that we can save a lot of trouble when we modify it.

So let's start our own View package.

The first step is to define your own attributes. Create the attrs.xml file in the values folder as follows:

The second step is to customize our own View

/ * * @ author: huangxianfeng on 2017-3-14. * step 2: create your own View * customize your own View,TopBar template, you cannot modify this interface in every interface * / public class Topbar extends RelativeLayout {private Button leftButton,rightButton; private TextView tvTitle; private int leftTextColor; private Drawable leftBackground; private String leftText; private int rightTextColor; private Drawable rightBackground; private String rightText; private float titleTextSize; private int titleTextColor Private String title; private LayoutParams leftParams,rightParams,titleParams; private topbarClickListener listener; / * create an interface for empty external calls * / public interface topbarClickListener {public void leftClick (); public void rightClick ();} public void setOnTopbarClickListener (topbarClickListener listener) {this.listener = listener } @ TargetApi (Build.VERSION_CODES.JELLY_BEAN) public Topbar (final Context context, AttributeSet attrs) {super (context, attrs); / / contains all your own custom attributes TypedArray ta = context.obtainStyledAttributes (attrs, R.styleable.Topbar); / / extract custom attributes leftTextColor = ta.getColor (R.styleable.Topbar_leftTextColor, 0) from attributes LeftBackground = ta.getDrawable (R.styleable.Topbar_leftBackground); leftText = ta.getString (R.styleable.Topbar_leftText); rightTextColor = ta.getColor (R.styleable.Topbar_rightTextColor, 0); rightBackground = ta.getDrawable (R.styleable.Topbar_rightBackground); rightText = ta.getString (R.styleable.Topbar_rightText); titleTextSize = ta.getDimension (R.styleable.Topbar_titleTextSize, 0) TitleTextColor = ta.getColor (R.styleable.Topbar_titleNameTextColor, 0); title = ta.getString (R.styleable.Topbar_titleName); ta.recycle (); / / Recycle / / create a custom control leftButton = new Button (context); rightButton = new Button (context); tvTitle = new TextView (context); / / set property leftButton.setTextColor (leftTextColor) LeftButton.setBackground (leftBackground); leftButton.setText (leftText); rightButton.setTextColor (rightTextColor); rightButton.setBackground (rightBackground); rightButton.setText (rightText); tvTitle.setTextColor (titleTextColor); tvTitle.setTextSize (titleTextSize); tvTitle.setText (title); tvTitle.setGravity (Gravity.CENTER); setBackgroundColor (0xFFF59563) LeftParams = new LayoutParams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); leftParams.addRule (RelativeLayout.ALIGN_PARENT_LEFT, TRUE); / / add a rule to display addView (leftButton, leftParams) on the left side of the control; / / complete the addition of a View control to ViewGroup and add rightParams = new LayoutParams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) RightParams.addRule (RelativeLayout.ALIGN_PARENT_RIGHT, TRUE); / / add a rule to display addView (rightButton, rightParams) on the right side of the control; / / complete the addition of a View control to ViewGroup with titleParams = new LayoutParams (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT); titleParams.addRule (RelativeLayout.CENTER_IN_PARENT, TRUE) / / add a rule to center the control to display addView (tvTitle, titleParams); / / add a View control to ViewGroup and add leftButton.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {/ / We only call the exposed method here to listener.leftClick () ); rightButton.setOnClickListener (new OnClickListener () {@ Override public void onClick (View v) {listener.rightClick ();}}) } / * sets whether the left button displays * @ param flag * / public void setLeftIsvisable (boolean flag) {if (flag) {leftButton.setVisibility (View.VISIBLE);} else {leftButton.setVisibility (View.GONE) }} / * sets whether the button on the right displays * @ param flag * / public void setRightIsvisable (boolean flag) {if (flag) {rightButton.setVisibility (View.VISIBLE);} else {rightButton.setVisibility (View.GONE);}

This View is very simple, just some simple functional implementation, there are methods you want to add, you can change them.

The third step is how to use our above content? In fact, it's simple, as long as you add it to our activity_main.xml layout.

/ / when adding your own custom attributes, you should first add a xmln:custom= "http://schemas.android.com/apk/res-auto" / / to reference your own attributes. The following scustom is the referenced own attributes.

In the above code, why is there a dependency on the package custom? It's just that custom properties depend on names, to distinguish them from android, but to add them to the outermost parent layout.

Xmlns:custom= "http://schemas.android.com/apk/res-auto"

To rely on attribute categories.

The fourth step is to use our Topbar in our MainActivity.

Public class MainActivity extends AppCompatActivity {private Topbar mTopbar; @ Override protected void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); initUI ();} private void initUI () {mTopbar = (Topbar) findViewById (R.id.topbar) MTopbar.setOnTopbarClickListener (new Topbar.topbarClickListener () {@ Override public void leftClick () {Toast.makeText (MainActivity.this, "leftClick", Toast.LENGTH_SHORT). Show ();} @ Override public void rightClick () {Toast.makeText (MainActivity.this, "rightClick", Toast.LENGTH_SHORT) .show () }}); mTopbar.setLeftIsvisable (true);}} at this point, the study on "how to customize TopBar and property encapsulation" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report