How to create a generic Android XML layout for all activities

前端 未结 3 1841
无人共我
无人共我 2020-12-16 02:12

I have an application that needs the same items [5 buttons acting as tabs] in every screen. Is it possible to create a \"base XML layout\" that has these 5 buttons and then

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-16 02:58

    Create a common layout for your base activity. and then include that layout in all the layout using the tagwhich you want to make the same.

    After that create one Abstract Activity and then handle all the click of the buttons and code in this activity and then extends this activity in all other activity in which you have include the base layout.

    For example

    common buttons xml layout

    
    
    
        

    Here is a xml layout in which you can include above XML file

    
    

    Here android:layout_width and android:layout_height and layout are compulsory attributes

    Now here is a Base Activity which handles the click of the common controls

    public abstract class BottomBar extends Activity implements OnClickListener {
    
        protected Button btnHome;
        Button btnSetting, btnMore;
        private Activity mActivity;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mActivity = this;
        }
    
        protected void mappingWidgets() {
    
            btnHome = (Button) findViewById(R.id.btnHome);
            btnSetting = (Button) findViewById(R.id.btnSetting);
            btnMore = (Button) findViewById(R.id.btnMore);
    
            btnHome.setOnClickListener(this);
            btnSetting.setOnClickListener(this);
            btnMore.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            if (v == null)
                throw new NullPointerException(
                        "You are refering null object. "
                                + "Please check weather you had called super class method mappingWidgets() or not");
            if (v == btnHome) {
    
            } else if (v == btnSetting) {
    
            }else if(v == btnMore) {
    
            }
        }
    
        protected void handleBackgrounds(View v) {
            if (v == btnHome) {
                btnHome.setBackgroundResource(R.drawable.bottom_btn_hover);
                btnSetting.setBackgroundResource(R.drawable.bottom_btn_active);
                btnMore.setBackgroundResource(R.drawable.bottom_btn_active);
    
            } else if (v == btnSetting) {
                btnHome.setBackgroundResource(R.drawable.bottom_btn_active);
                btnSetting.setBackgroundResource(R.drawable.bottom_btn_hover);
                btnMore.setBackgroundResource(R.drawable.bottom_btn_active);
    
            } else if (v == btnMore) {
                btnHome.setBackgroundResource(R.drawable.bottom_btn_active);
                btnSetting.setBackgroundResource(R.drawable.bottom_btn_active);
                btnMore.setBackgroundResource(R.drawable.bottom_btn_hover);
            }
        }
    
    }
    

    Now one step remaining is to extend this Base activity in all your activities.

    You can extend the Base activity in an activity using the extends keyword. For example

    public class MyActivity extends BottomBar
    

    Note: From the child activity you must call the super method of the base class to handle the click of the common controls of your base layout.

    You can thus implement multiple common layout within your single activity.

    Hope this will help you. Enjoy!!

提交回复
热议问题