Android custom Composite Control-Multi-button switching at the bottom
:
Most software on the market is similar to the above structure. There are several buttons at the bottom to switch to different interfaces. Based on the OOP idea, I want to encapsulate the entire layout below into a class, that is, our custom Composite Control-multiple buttons at the bottom to switch the layout. I call it BottomLayout. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + logs/LFxcHQo6zO0sPHz8i/tNK7z8KyvL7WPGJyIC8 + CjxpbWcgYWx0PQ = "here write picture description" src = "http://www.bkjia.com/uploads/allimg/150213/04111W943-1.png" title = "\"/>
The outermost LinearLayout direction is horizontal, followed by five RelativeLayout with the same weight. Each RelativeLayout contains a Button (used to display the selected status) and an ImageView (used to display the red dot)
The following is the code.
Package comzyh. bottomlayout; import android. content. context; import android. util. attributeSet; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. linearLayout;/*** button switching layout at the bottom of the main interface ** @ version 1.0 * @ author zyh */public class BottomLayout extends LinearLayout implements OnClickListener {public BottomLayout (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr); init (context);} public BottomLayout (Context context, AttributeSet attrs) {this (context, attrs, 0 );} public BottomLayout (Context context) {this (context, null, 0);} private Context context; private View rl_home, rl_encounter, rl_community, rl_message, rl_user; private View iv_new_message, iv_new_home; private Button btn_home, btn_encounter, btn_community, btn_message, btn_user; private OnItemClickListener listener; private int currentPosition = 0; /*** initialize *** @ version 1.0 * @ author zyh * @ param context */private void init (Context context) {this. context = context; LayoutInflater. from (context ). inflate (R. layout. layout_bottom, this); // layout of 5 equal points rl_home = findViewById (R. id. rl_home); rl_encounter = findViewById (R. id. rl_encounter); rl_community = findViewById (R. id. rl_community); rl_message = findViewById (R. id. rl_message); rl_user = findViewById (R. id. rl_user); // 5 buttons btn_home = (Button) findViewById (R. id. btn_home); btn_encounter = (Button) findViewById (R. id. btn_encounter); btn_community = (Button) findViewById (R. id. btn_community); btn_message = (Button) findViewById (R. id. btn_message); btn_user = (Button) findViewById (R. id. btn_user); // small dot iv_new_home = findViewById (R. id. iv_new_home); iv_new_message = findViewById (R. id. iv_new_message); hideHomeCircle (); hideMessageCircle (); changeButtonStatus (0); // the default position is 0 setListener ();} /*** display the red circle next to the homepage button ** @ version 1.0 * @ author zyh */public void showHomeCircle () {iv_new_home.setVisibility (View. VISIBLE);}/*** hide the red circle next to the home page button ** @ version 1.0 * @ author zyh */public void hideHomeCircle () {iv_new_home.setVisibility (View. GONE);}/*** display the red circle next to the home page button ** @ version 1.0 * @ author zyh */public void showMessageCircle () {iv_new_message.setVisibility (View. VISIBLE);}/*** hide the red circle next to the home page button ** @ version 1.0 * @ author zyh */public void hideMessageCircle () {iv_new_message.setVisibility (View. GONE);}/*** for button design press listener ** @ version 1.0 * @ author zyh */private void setListener () {rl_home.setOnClickListener (this); rl_encounter.setOnClickListener (this ); rl_community.setOnClickListener (this); rl_message.setOnClickListener (this); rl_user.setOnClickListener (this );} /*** provides the interface for setting external click items ** @ version 1.0 * @ author zyh * @ param listener */public void setOnItemClickListener (OnItemClickListener listener) {this. listener = listener;} public interface OnItemClickListener {public void onItemClick (int position);} @ Override public void onClick (View v) {currentPosition = 0; switch (v. getId () {case R. id. rl_home: currentPosition = 0; break; case R. id. rl_encounter: currentPosition = 1; break; case R. id. rl_community: currentPosition = 2; break; case R. id. rl_message: currentPosition = 3; break; case R. id. rl_user: currentPosition = 4; break;} if (listener = null) {return;} listener. onItemClick (currentPosition); changeButtonStatus (currentPosition );} /*** change the button selection status based on the current position ** @ version 1.0 * @ author zyh * @ param position */private void changeButtonStatus (int position) {btn_home.setSelected (position = 0); btn_encounter.setSelected (position = 1); btn_community.setSelected (position = 2); btn_message.setSelected (position = 3 ); btn_user.setSelected (position = 4 );}}
Set five RelativeLayout as the click area to prevent the click from responding in some places. Set it to the Button (the Button's background may be deformed). The Listener that encapsulates the Item click provides a method to hide small dots.
The above layout can be optimized.
View the layout level and find that there is a layer of LinearLayout in the middle. Why?
Because our BottomLayout inherits from LinearLayout, And we write
LayoutInflater.from(context).inflate(R.layout.layout_bottom, this);
How can I optimize another layout?
Use merge nodes to remove redundant nodes
So we can write the layout file as follows:
For more information about merge, see the optimization of merge tag Analysis in my Android view.
Below is the main interface
Use custom composite controls
Public class MainActivity extends Activity {private BottomLayout layout_bottom; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); layout_bottom = (BottomLayout) findViewById (R. id. layout_bottom); layout_bottom.setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (int position) {Toast. makeText (MainActivity. this, click + position, 0 ). show () ;}}); new Handler (). postDelayed (new Runnable () {public void run () {layout_bottom.showHomeCircle () ;}, 5000); new Handler (). postDelayed (new Runnable () {public void run () {layout_bottom.showMessageCircle () ;}, 8000 );}}