1. 程式人生 > >Activity繼承BaseActivity的使用(有相同不狀態列時很適用)

Activity繼承BaseActivity的使用(有相同不狀態列時很適用)

首先在BaseActivity中寫好其他Activity要繼承的狀態列的佈局:
佈局檔案activity_base.xml程式碼如下:

<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" android:orientation="vertical" tools:context="com.chuangzhi.chuangzhi360.activity.BaseActivity">
<RelativeLayout android:layout_width="match_parent" android:layout_height="50px" android:background="@drawable/top_bg"> <ImageView android:id
="@+id/logo" android:layout_width="40px" android:layout_height="40px" android:layout_centerVertical="true" android:layout_marginLeft="20px" android:src="@drawable/yimei_logo"/>
<TextView android:id="@+id/company_name" android:layout_width
="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/logo" android:layout_marginLeft="20px" android:text="@string/ymys" android:textSize="16px" android:textColor="@color/white"/>
<TextView android:id="@+id/date" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_alignParentRight="true" android:layout_marginRight="20px" android:textSize="16px" android:textColor="@color/white"/> <ImageView android:id="@+id/is_connect" android:layout_width="22px" android:layout_height="22px" android:src="@drawable/connect" android:layout_toLeftOf="@+id/date" android:layout_centerVertical="true" android:layout_marginRight="10px"/> </RelativeLayout> <!-- 為下個佈局分配空間 --> <RelativeLayout android:id="@+id/all_content" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000"> </RelativeLayout> </LinearLayout>

接下來BaseActivity.java的程式碼:

public class BaseActivity extends AppCompatActivity {

    TextView shopName;
    TextView date;
    /**
     * 判斷當前介面的那個介面
     */
    ImageView isConnect;
    private RelativeLayout allContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideStatusBar();   //影響狀態列
        setContentView(R.layout.activity_base);
    }

    //  初始化
    public void baseInit(){
        date = (TextView) findViewById(R.id.date);
        isConnect = (ImageView) findViewById(R.id.is_connect);

        showDate();
        showNetwork();
    }

    //  設定要顯示的佈局方法
    public void BaseSetContentView(int layoutID){
        allContent = (RelativeLayout)findViewById(R.id.all_content);
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//      把繼承該BaseActivity的layoutID放進來 顯示
        View view = inflater.inflate(layoutID, null);
        allContent.addView(view);
    }

    private void hideStatusBar() {
        // TODO TODO TODO TODO Auto-generated method stub
        // 隱藏標題
        requestWindowFeature(Window. FEATURE_NO_TITLE );
        // 定義全屏引數
        int flag = WindowManager.LayoutParams. FLAG_FULLSCREEN ;
        // 獲得視窗物件
        Window myWindow = this.getWindow();
        // 設定 Flag 標識
        myWindow.setFlags(flag,flag);
    }

    /**
     * 顯示網路狀態
     */
    private void showNetwork() {
        ConnectivityManager mConnectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (mNetworkInfo != null) {   //網路可用
            isConnect.setBackgroundResource(R.drawable.connect);
        }else {
            isConnect.setBackgroundResource(R.drawable.un_connect);
        }
    }

    private void showDate() {
        final Handler mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.arg1==1){
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                    String dateStr = format.format(new Date());
                    date.setText(dateStr);
                }
            }
        };
        //定義一個計時器,設定為延遲0ms後執行,每隔1min執行一次(這裡如果設定為 timer.schedule(task,1000)表示執行一次)
        new Timer().schedule(new TimerTask(){
            @Override
            public void run() {
                Message message = new Message();
                message.arg1 = 1;
                mHandler.sendMessage(message);
            }
        }, 0, 1000);
    }

}

接下來就是要繼承BaseActivity的類,這裡就舉一個例好了:

public class MainActivity extends BaseActivity implements View.OnClickListener {

    public ImageView[] tabImages;
    public TextView[] tabTexts;
    public LinearLayout[] tabLayouts;
    public static Fragment currentFragment;
    public static FragmentManager manager;
    private RelativeLayout infoLayout;
    private ImageView contactBook;
    private ImageView setting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//      呼叫父類方法顯示view
        BaseSetContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        baseInit();
        //後面就是你的Activity裡其他控制元件的初始化
    }