Common Header in different activities using BaseActivity in android

前端 未结 4 1510
傲寒
傲寒 2020-12-03 05:10

I want write code once and use in different activities. I have created a Base Activity class for that . Also the header of all the layouts in different activiti

相关标签:
4条回答
  • 2020-12-03 05:37

    I think you should achieve it using Fragment, this may helps you.

    1 - in main.xml, add:

    <fragment
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        class="com.package.name.HeaderPanel" />
    
    //remaining is same 
    

    2 - the BaseActivity which extends FragmentActivity:

    public class BaseActivityMenu extends FragmentActivity {
    
        private static final String TAG = Default.class.getName() + " - ";
        private int mResLayoutId;
    
        public void onCreate(Bundle savedInstanceState, int resLayout){
            super.onCreate(savedInstanceState);
            setContentView(resLayout);
            mResLayoutId = resLayout;
            switch(mResLayoutId){
                // here change with your xml file
                case R.layout.home:
                    // set here common control like header textview
                    break;
                default:
                    break;
            }
        }
    }
    

    3 - Now, you can extend your Activity with the BaseActivity. This will allow the Activity to be extended by FragmentActivity:

    public class ABCActivity extends BaseActivityMenu {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState, R.layout.home);
        }
    }
    
    0 讨论(0)
  • 2020-12-03 05:45

    If you are making inheritance with activities and your base activity calls setContentView and after that the real activity calls setContentView the last call will set the layout for activity. So if you are looking for a solution where all activies have the same header component the are 2 ways.

    1. For each activity layout xml you include that component

    2. -You make function for baseActivity e.g. setContent(int layout_id) -You call that with your activity always. -Baseactivity inflates a root view with header and inflates layout_id view to that layout. -Then calls the actual setContentView with that component.

    0 讨论(0)
  • 2020-12-03 05:57

    For this you have to create one header.xml which will be included in each and every layout for your activities as follows

    header.xml

    <RelativeLayout>
      <TextView android:id="@+id/txtHeading"
          .... />
    </RelativeLayout>
    

    activity_main.xml

    <RelativeLayout>
      <!-- include your header here -->
      <include layout="@layout/header"
         ... />
    
      <!-- Rest of your views -->
    
    </RelativeLayout>
    

    BaseActivity

    abstract class BaseActivity extends Activity {
      protected TextView txtHeading;
      public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
      }
    
    
      protected void setHeading(int resId) {
         if(txtHeading == null)
         txtHeading = findViewById(R.id.txtHeading);
         if(txtHeading != null)
           txtHeading.setText(resId);
      }
    }
    

    MainActivity

    class MainActivity extends BaseActivity {
       public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          setHeading(R.string.heading_main);
       }
    }
    

    You can put as many views you want and manage common things in BaseActivity or BaseListActivity.

    0 讨论(0)
  • 2020-12-03 06:02

    In code, your base activity is called ExampleActivity, but in your child class you are extending BaseActivityMenu. Don't know where its coming from.

    Perhaps change:

    public class ABCActivity extends BaseActivityMenu
    

    To this:

    public class ABCActivity extends ExampleActivity
    

    Moreover, I would suggest you to define your base activity (ExampleActivity) as an Abstract class. For example:

    public abstract class ExampleActivity extends Activity
    

    Doing so will not define your base class as concrete and will make it easier to debug in case of problems.

    0 讨论(0)
提交回复
热议问题