创建headerledout标题?

时间:2015-09-04 22:50:17

标签: android drawerlayout

我正在尝试为DrawerLayout创建一个Header。当我添加<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/dl" android:background="@drawable/splash" > <FrameLayout android:id="@+id/fl" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> <LinearLayout android:layout_width="250dp" android:layout_height="match_parent" android:orientation="vertical"> <ImageView android:layout_width="150dp" android:layout_height="150dp" android:src="@drawable/logo" /> <ListView android:id="@+id/lv" android:layout_width="250dp" android:layout_height="match_parent" android:choiceMode="singleChoice" android:divider="#DCDCDC" android:paddingLeft="5dp" android:paddingRight="5dp" android:dividerHeight="1dp" android:background="@color/action_bar" android:layout_gravity="start" > </ListView> </LinearLayout> </android.support.v4.widget.DrawerLayout> 时会抛出异常并停止应用。

我怎么能这样做?

XML

public class CustomDrawerLayout extends ActionBarActivity implements OnItemClickListener{
    private ActionBar ab;
    private DrawerLayout dl;
    private ListView lv;
    private ActionBarDrawerToggle tg;

    private List<ItensListView> fragments;
    private CharSequence tl; //titulo principal
    private CharSequence tlf; //titulo fragment

    public static final String APP_NAME = "App";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_drawerlayout);
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.action_bar)));
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.actionbar_custom);

        init();

        if(savedInstanceState == null){
           selectedItem(0);
        }

    }



    private void init(){
        //actionbar
        onConfigActionBar();
        //listview
        configItensListView();
        lv = (ListView)findViewById(R.id.lv);               
        lv.setAdapter(new DrawerLayoutListViewAdapter(this, fragments));
        lv.setOnItemClickListener(this);        
        //drawerlayout
        dl = (DrawerLayout)findViewById(R.id.dl);
        //actionbardrawertoggle
        tg = new ActionBarDrawerToggle(this, dl, R.drawable.ic_launcher, R.string.drawer_open){
            public void onDrawerClosed(View view) {
                ab.setTitle(tl);
                supportInvalidateOptionsMenu();
            }

            public void onDrawerOpened(View view) {
                ab.setTitle(tlf);
                supportInvalidateOptionsMenu();
            }
        };
        dl.setDrawerListener(tg);
        tl = tlf = getTitle();
    }

    /** ativa actionbar e botao home na action bar */
    private void onConfigActionBar(){
        ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setHomeButtonEnabled(true);
    }




    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        tg.onConfigurationChanged(newConfig);
    }

    /** necessario */
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        tg.syncState();
    }


     @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         if (tg.onOptionsItemSelected(item)) {
                return true;
         }
         return super.onOptionsItemSelected(item);
     }



     /** necessario */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.custom_drawer_layout, menu);

        return true;
    }

    /** necessario */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        boolean status = dl.isDrawerOpen(lv);
        menu.findItem(R.id.action_settings).setVisible(!status);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
        Log.i("POSITION->", position + "");
        selectedItem(position);     
    }

    /** seleciona o fragment q sera usado */
    private void selectedItem(int position){
        FragmentTransaction ft;
        Fragment frag;
        switch (position){
            case 0:
                ft = getSupportFragmentManager().beginTransaction();
                frag = NoticiaFrag.newInstance();
                ft.replace(R.id.fl, frag);
                ft.addToBackStack("NoticiaFrag");
                ft.commit();
                break;
            case 1:
                ft = getSupportFragmentManager().beginTransaction();
                frag = EventoFrag.newInstance();
                ft.replace(R.id.fl, frag);
                ft.addToBackStack(APP_NAME);
                ft.commit();
                break;
            case 2:
                ft = getSupportFragmentManager().beginTransaction();
                frag = LoginFrag.newInstance();
                ft.replace(R.id.fl, frag);
                ft.addToBackStack(APP_NAME);
                ft.commit();
                break;
            case 3:
                ft = getSupportFragmentManager().beginTransaction();
                frag = ContatoFrag.newInstance();
                ft.replace(R.id.fl, frag);
                ft.addToBackStack(APP_NAME);
                ft.commit();
                break;
            case 4:
                ft = getSupportFragmentManager().beginTransaction();
                frag = CompartilhaFrag.newInstance();
                ft.replace(R.id.fl, frag);
                ft.addToBackStack(APP_NAME);
                ft.commit();
                break;
            case 5:
                ft = getSupportFragmentManager().beginTransaction();
                frag = SobreFrag.newInstance();
                ft.replace(R.id.fl, frag);
                ft.addToBackStack(APP_NAME);
                ft.commit();
                break;
            default:
                closeApp();
                break;

        }

        lv.setItemChecked(position, true);
        setCustomTitle(fragments.get(position).getTexto());
        dl.closeDrawer(lv);
    }

    /** define o titulo da actionbar */
    private void setCustomTitle(String title){
        //SpannableString s = new SpannableString(title);
       // s.setSpan(new TypefaceSpan(this, BatalhaConfigs.FONT), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        //ab.setTitle(s);
        //tl = s;
    }

    @Override
    public void onBackPressed() {
        if(getSupportFragmentManager().getBackStackEntryCount() > 0){
            getSupportFragmentManager().popBackStackImmediate();
        }else{
            super.onBackPressed();

        }
    }


    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
    }

    /** Configura o List com as informacoes **/
    private void configItensListView(){
        fragments = new ArrayList<ItensListView>();
        //define
        ItensListView itens0 = new ItensListView("Noticias", R.drawable.setavermelha);
        ItensListView itens1 = new ItensListView("Eventos", R.drawable.setavermelha);
        ItensListView itens2 = new ItensListView("Área do Aluno", R.drawable.setavermelha);
        ItensListView itens3 = new ItensListView("Contato", R.drawable.setavermelha);
        ItensListView itens4 = new ItensListView("Redes Sociais", R.drawable.setavermelha);
        ItensListView itens5 = new ItensListView("Sobre", R.drawable.setavermelha);
        ItensListView itens6 = new ItensListView("Sair", R.drawable.setavermelha);

        //add
        fragments.add(itens0);
        fragments.add(itens1);
        fragments.add(itens2);
        fragments.add(itens3);
        fragments.add(itens4);
        fragments.add(itens5);
        fragments.add(itens6);


        //AreaAlunoFrag

    }

    private void closeApp(){
        System.exit(0);
    }



    @Override
    protected void onResume() {
        PushControl.setIsVisible(true);
        super.onResume();
    }

    @Override
    protected void onPause() {
        PushControl.setIsVisible(false);
        super.onPause();
    }

    @Override
    protected void onStop() {
        PushControl.setIsVisible(false);
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        PushControl.setIsVisible(false);
        super.onDestroy();
        CustomVolleySingleton.getInstance().cancelPendingRequests(CustomVolleySingleton.TAG);
    }
}

ActionBarActivity

java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.ferpapps.santaluzapp/br.com.ferpapps.santaluzapp.menu.CustomDrawerLayout}: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
            at android.app.ActivityThread.access$700(ActivityThread.java:159)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
            at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:1100)
            at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1305)
            at br.com.ferpapps.santaluzapp.menu.CustomDrawerLayout.selectedItem(CustomDrawerLayout.java:210)
            at br.com.ferpapps.santaluzapp.menu.CustomDrawerLayout.onCreate(CustomDrawerLayout.java:66)
            at android.app.Activity.performCreate(Activity.java:5372)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
            at android.app.ActivityThread.access$700(ActivityThread.java:159)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:176)
            at android.app.ActivityThread.main(ActivityThread.java:5419)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
            at dalvik.system.NativeStart.main(Native Method)

异常

{{1}}

1 个答案:

答案 0 :(得分:2)

试试这个:

<LinearLayout
    android:id="@+id/navdrawer"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
       ...
        />
    <ListView
       ...
        >
    </ListView>
</LinearLayout>

LinearLayout ll = (LinearLayout) findViewById(R.id.navdrawer);

...

dl.isDrawerOpen(ll);

...

dl.closeDrawer(ll);