Android导航抽屉抽象
我知道以前有人问过这个问题(另外两个我也发现了类似的问题),但他们没有解决这个问题 下面是场景,我创建了一个类,如下所示:Android导航抽屉抽象,android,inheritance,layout,abstraction,Android,Inheritance,Layout,Abstraction,我知道以前有人问过这个问题(另外两个我也发现了类似的问题),但他们没有解决这个问题 下面是场景,我创建了一个类,如下所示: public class BaseActivity extends ActionBarActivity { // Implemeted the Actionbar and navigation drawer here! // and it containts onCreate method and setContentView } BaseActivity XM
public class BaseActivity extends ActionBarActivity {
// Implemeted the Actionbar and navigation drawer here!
// and it containts onCreate method and setContentView
}
BaseActivity XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/base_nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<ListView
android:id="@+id/base_left_drawer"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#dddddd"/>
<ListView
android:id="@+id/base_right_drawer"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#dddddd"/>
</android.support.v4.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/stream_new_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hi dude"/>
</LinearLayout>
以下是XML的主要活动:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/base_nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<ListView
android:id="@+id/base_left_drawer"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#dddddd"/>
<ListView
android:id="@+id/base_right_drawer"
android:layout_width="320dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#dddddd"/>
</android.support.v4.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/stream_new_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hi dude"/>
</LinearLayout>
因此,现在我可以在我的主活动中看到Actionbar和导航抽屉,但是,如果我尝试在我的主活动上使用
setContectView(R.layout.Main)
来使用我为该活动创建的xml布局,导航抽屉将不再显示;我猜是因为主活动上的onCreate
和setContectView
重写了我从中扩展的BaseActivity中的相同语句。有什么解决方法吗?我希望我正确地理解了你的意思:你在BaseActivity
中使用setContentView()
设置抽屉,在main Activity
中使用setContentView()
进行其余的布局设置?
如果是这样的话,那就不行了。第二次执行setContentView()
时,它将替换旧布局,然后就没有抽屉了。
在您的main活动的setContentView()
中使用的布局中必须有一个抽屉。
另外,不要使用setContentView()
两次,第一次是没有用的 您能否发布这两个活动的onCreate方法的完整实现?我无法想象ActionBar会完全消失……我的错!操作栏出现。只有导航抽屉无法工作/显示。因此,要清楚,我不应该在我的BaseActivity中使用“setContentView()”,而且,我必须使用我在上面发布的BaseActivity XML作为我的主要活动的XML布局?是的,试试看。如果你想有一个抽屉,在你的主要活动中使用的布局中必须有一个,是的。