处理相同的导航抽屉,但更改每个活动的标题文本

时间:2014-02-17 04:30:21

标签: android

我们是否可以在每项活动中使用相同的导航抽屉,但标题文字会有变化?例如,我有两个活动,我的家庭活动将显示标题文本作为主页,我的“关于我们”活动将显示“关于我们”标题文本,但所有活动都将具有相同的导航设置。

BaseActivity

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
 public class BaseActivity extends Activity {

ExpandableListAdapter listAdapter;
ExpandableListView expListView;

public DrawerLayout drawer;
ImageView navDrawerBtn;

HashMap<String, List<String>> listDataChild;
List<String> listDataHeader;
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

protected LinearLayout fullLayout;
protected FrameLayout actContent;

@Override
public void setContentView(final int layoutResID) {
    fullLayout= (LinearLayout) getLayoutInflater().inflate(R.layout.home, null); // Your base layout here
    actContent= (FrameLayout) fullLayout.findViewById(R.id.content_frame);
    getLayoutInflater().inflate(layoutResID, actContent, true); // Setting the content of layout your provided to the act_content frame
    super.setContentView(fullLayout);
    // here you can get your drawer buttons and define how they should behave and what must they do, so you won't be needing to repeat it in every activity class

    prepareListData();

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }


    navDrawerBtn = (ImageView)findViewById(R.id.headerDrawer);
    expListView = (ExpandableListView) findViewById(R.id.lvExp);

    if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
        expListView.setIndicatorBounds(402,465);    
    } else {        
        expListView.setIndicatorBoundsRelative(402,465);    
    } 

    drawer = (DrawerLayout)findViewById(R.id.drawer_layout);


    navDrawerBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            if(!drawer.isDrawerOpen(expListView)) {
                drawer.openDrawer(expListView);
                } else {
                    drawer.closeDrawer(expListView);
                }

            }
        });


    //listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);

    // Listview Group click listener
    expListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {
            // Toast.makeText(getApplicationContext(),
            // "Group Clicked " + listDataHeader.get(groupPosition),
            // Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    // Listview Group expanded listener
    expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " Expanded",
                    Toast.LENGTH_SHORT).show();
        }
    });

    // Listview Group collasped listener
    expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(getApplicationContext(),
                    listDataHeader.get(groupPosition) + " Collapsed",
                    Toast.LENGTH_SHORT).show();

        }
    });

    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {

            switch (childPosition) {
            case 0: 
                Intent a = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(a);
                break;

            case 1: 
                Intent b = new Intent(getApplicationContext(), ScheduleActivity.class);
                startActivity(b);
                break;
}
            return false;
            // TODO Auto-generated method stub

        }
    });
 }


/*
 * Preparing the list data
 */
protected void prepareListData() {


    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();

    // Adding child data
    listDataHeader.add("VRP Medical Bay");
    //listDataHeader.add("");
    //listDataHeader.add("");

    // Adding child data

    List<String> listUnderVRP = new ArrayList<String>();

    listUnderVRP.add("eDataClinical");
    listUnderVRP.add("Schedule");
    listUnderVRP.add("Dictate");
    listUnderVRP.add("View Messages");
    listUnderVRP.add("Reports for Signature");
    listUnderVRP.add("View Billing");
    listUnderVRP.add("View State");


    listDataChild.put(listDataHeader.get(0), listUnderVRP); // Header, Child data
    //listDataChild.put(listDataHeader.get(1), nowShowing);
    //listDataChild.put(listDataHeader.get(2), comingSoon);


}



}

我的家庭布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="73dp"
android:background="@color/actionbar" >

<ImageView
    android:id="@+id/headerDrawer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/ic_drawer"
    android:contentDescription="@string/desc" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/actionHeaderText"
    android:layout_toRightOf="@+id/headerDrawer"
    android:src="@drawable/e_icon"
    android:contentDescription="@string/desc"  />

<TextView
    android:id="@+id/actionHeaderText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/imageView1"
    android:text="@string/actionbar_title"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="@color/white"
    android:textSize="32sp"
    android:textStyle="bold"
    android:typeface="monospace" />

    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->

     <ExpandableListView
        android:id="@+id/lvExp"
        android:layout_width="470dp"
        android:layout_height="match_parent"
        android:groupIndicator="@drawable/group_selector"
         android:transcriptMode="alwaysScroll"
        android:layout_gravity="start"
        android:childDivider="#4abcd7"
       android:divider="#626262"
         android:dividerHeight="4dp"

        android:cacheColorHint="#fff" >
    </ExpandableListView>
</android.support.v4.widget.DrawerLayout>

</LinearLayout>

我的结算活动:

public class ViewBillingActivity extends BaseActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_billing_test);


}

我的结算布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="73dp"
    android:background="@color/actionbar" >

    <ImageView
        android:id="@+id/headerDrawer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/desc"
        android:src="@drawable/ic_drawer" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/actionHeaderText"
        android:layout_toRightOf="@+id/headerDrawer"
        android:src="@drawable/e_icon" />

    <TextView
        android:id="@+id/actionHeaderText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/imageView1"
        android:text="@string/view_billing"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/white"
        android:textSize="32sp"
        android:textStyle="bold"
        android:typeface="monospace" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="90dp"
    android:background="@color/view_billing_bg"
    android:paddingTop="10dp" >

    <TextView
        android:id="@+id/latestBillingTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/latest_billing"
        android:textColor="@color/white"
        android:textSize="34sp"
        android:textStyle="normal" />

    <TextView
        android:id="@+id/latestBillingDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/latestBillingTitle"
        android:layout_centerHorizontal="true"
        android:text="(03/01/2014 - 03/31/2014)"
        android:textColor="@color/white"
        android:textSize="24sp" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="720dp"
    android:layout_height="820dp"
    android:layout_below="@+id/relativeLayout1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="35dp"
    android:background="@color/white"
    android:paddingBottom="50dp" >

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="0dp"
        android:background="@color/white" >

        <RelativeLayout
            android:id="@+id/relativeLayout7"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="@color/view_mssgs_id_bg" >

            <TextView
                android:id="@+id/vbTxtID"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:gravity="center_horizontal"
                android:text="024"
                android:textColor="@color/white"
                android:textSize="24sp"
                android:textStyle="bold" />
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="500dp"
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="4dp"
            android:layout_toRightOf="@+id/relativeLayout7"
            android:background="@color/tile_box" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:text="@string/vb_outstanding_balance"
                android:textColor="@color/white"
                android:textSize="24sp"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/vbTxTOutBalance"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBaseline="@+id/textView1"
                android:layout_alignBottom="@+id/textView1"
                android:layout_marginLeft="23dp"
                android:layout_toRightOf="@+id/textView1"
                android:text="0.00"
                android:textColor="@color/white"
                android:textSize="24sp" />

        </RelativeLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/relativeLayout3"
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/relativeLayout2"
        android:layout_marginTop="4dp"
        android:background="@color/tile_box" >

        <TextView
            android:id="@+id/vbTxtLastMonthBill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="23dp"
            android:layout_toRightOf="@+id/textView1"
            android:text="2004.50"
            android:textColor="@color/white"
            android:textSize="24sp" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/vbTxtLastMonthBill"
            android:layout_alignBottom="@+id/vbTxtLastMonthBill"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="11dp"
            android:text="@string/vb_last_month_bill"
            android:textColor="@color/white"
            android:textSize="24sp"
            android:textStyle="bold" />

    </RelativeLayout>

     <RelativeLayout
         android:id="@+id/relativeLayout4"
         android:layout_width="fill_parent"
         android:layout_height="45dp"
         android:layout_alignParentLeft="true"
         android:layout_below="@+id/relativeLayout3"
         android:layout_marginTop="4dp"
         android:background="@color/tile_box" >

         <TextView
             android:id="@+id/vbTxtPaymentReceived"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerVertical="true"
             android:layout_marginLeft="23dp"
             android:layout_toRightOf="@+id/textView1"
             android:text="2004.50"
             android:textColor="@color/white"
             android:textSize="24sp" />

         <TextView
             android:id="@+id/textView1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignBaseline="@+id/vbTxtPaymentReceived"
             android:layout_alignBottom="@+id/vbTxtPaymentReceived"
             android:layout_alignParentLeft="true"
             android:layout_marginLeft="11dp"
             android:text="@string/vb_payments_received"
             android:textColor="@color/white"
             android:textSize="24sp"
             android:textStyle="bold" />
     </RelativeLayout>

      <RelativeLayout
         android:id="@+id/relativeLayout5"
         android:layout_width="fill_parent"
         android:layout_height="45dp"
         android:layout_alignParentLeft="true"
         android:layout_below="@+id/relativeLayout4"
         android:layout_marginTop="4dp"
         android:background="@color/tile_box" >

         <TextView
             android:id="@+id/vbTxtPaymentReceivedDate"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerVertical="true"
             android:layout_marginLeft="23dp"
             android:layout_toRightOf="@+id/textView1"
             android:text="04/05/2014"
             android:textColor="@color/white"
             android:textSize="24sp" />

         <TextView
             android:id="@+id/textView1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignBaseline="@+id/vbTxtPaymentReceivedDate"
             android:layout_alignBottom="@+id/vbTxtPaymentReceivedDate"
             android:layout_alignParentLeft="true"
             android:layout_marginLeft="11dp"
             android:text="@string/vb_payment_received_date"
             android:textColor="@color/white"
             android:textSize="24sp"
             android:textStyle="bold" />
     </RelativeLayout>

      <RelativeLayout
         android:id="@+id/relativeLayout6"
         android:layout_width="fill_parent"
         android:layout_height="45dp"
         android:layout_alignParentLeft="true"
         android:layout_below="@+id/relativeLayout5"
         android:layout_marginTop="4dp"
         android:background="@color/tile_box" >

         <TextView
             android:id="@+id/vbTxtBalance"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_centerVertical="true"
             android:layout_marginLeft="23dp"
             android:layout_toRightOf="@+id/textView1"
             android:text="1575.75"
             android:textColor="@color/white"
             android:textSize="24sp" />

         <TextView
             android:id="@+id/textView1"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_alignBaseline="@+id/vbTxtBalance"
             android:layout_alignBottom="@+id/vbTxtBalance"
             android:layout_alignParentLeft="true"
             android:layout_marginLeft="11dp"
             android:text="@string/vb_balance"
             android:textColor="@color/white"
             android:textSize="24sp"
             android:textStyle="bold" />
     </RelativeLayout>

</RelativeLayout>

 </RelativeLayout>

2 个答案:

答案 0 :(得分:0)

首先,根据design guidelines,最好在导航抽屉打开时在ActionBar中设置标题文本,而不是在导航抽屉布局中设置单独的TextView标题。

但是如果您真的需要在导航抽屉中单独显示“标题”文本 - 导航抽屉的布局(以及与之交互的方式)与您在活动中通常拥有的任何其他常规布局完全相同。只需声明TextView,给它一些合理的ID,在您的活动中获取此文本视图(findViewById())并设置正确的文字

答案 1 :(得分:0)

根据您要显示的文字,在onDrawerClosedonDrawerOpened方法中编写更改文本代码..