如何显示子活动的布局?

时间:2016-05-01 18:09:09

标签: android android-layout android-fragments android-activity

我创建的BaseActivity只创建了NavigationDrawer

然后我创建了TestActivity,扩展了BaseActivity

TestActivity成功绘制了其父活动中添加的Drawer

现在TestActivity有一些自己的功能。我在其中添加了几个按钮,但它没有显示自己的内容。

我在网上搜索了这个并在我的代码中添加了这些行,但它仍然没有任何区别。

它显示headerNavigationDrawer

的空白活动
public class TestActivity extends BaseNavigationActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      /*  setContentView(R.layout.activity_test);*/

        ViewGroup content = (ViewGroup) findViewById(R.id.base_layout);
        getLayoutInflater().inflate(R.layout.activity_test, content, true);

    } 

BaseActivity布局XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:fitsSystemWindows="true"
    tools:context="com.tixsee.mavs.HomeActivity"
    android:id="@+id/drawer_layout_main"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/base_layout">

        <include layout="@layout/header_layout"/>


    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/navigation_view"
        android:layout_gravity="start"
        android:background="@drawable/sidebg"
        app:menu="@menu/drawer_menu"
        app:itemTextColor= "#ffffff"
        app:headerLayout="@layout/navigation_drawer_header"
        android:gravity="bottom|left"
        android:dividerHeight="0dp"
        app:itemIconTint="@android:color/white"
        >

    </android.support.design.widget.NavigationView>


</android.support.v4.widget.DrawerLayout>

BaseClassNavigation.Java

public abstract BaseNavigationActivity扩展了AppCompatActivity {

static DrawerLayout drawerLayout;
FrameLayout headerLayout;
LinearLayout homeLayout;
FragmentTransaction fragmentTransaction;
NavigationView navigationView;
Button btnOpenDrawer;
Boolean displayHeader= Boolean.TRUE;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout_main);
    headerLayout= (FrameLayout) findViewById(R.id.header_layout);
    btnOpenDrawer= (Button) findViewById(R.id.btn_openDrawer);
    homeLayout= (LinearLayout) findViewById(R.id.home_layout);

    btnOpenDrawer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            drawerLayout.openDrawer(Gravity.LEFT);
        }
    });


    getNavigationControls();
  }
  

GetNavigationControls包含所有switch-case语句   打开请求的Activityfragment

请指导我如何显示子类布局内容。

2 个答案:

答案 0 :(得分:3)

制作您的BaseActivity abstract

定义一个抽象方法,从子类中获取布局res。

protected abstract int getLayoutRes();

在您的测试活动中,实现您的抽象方法getLayoutRes();,如下所示

@Override
protected int getLayoutRes() {
    return R.id.base_layout;
}

在您的BaseActivity onCreate方法中,让您的子布局对其进行充气并添加到您的容器中,如下所示:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_parent_activity_layout_with_drawer);

        ViewGroup content = (ViewGroup) findViewById(R.id.base_layout);
        getLayoutInflater().inflate(getLayoutRes(),content,true);

    } 

使用这种方法,您可以添加更多活动,扩展BaseActivity只需实现抽象getLayoutRes()方法。

祝你好运..

修改:这是您的BaseActivity代码

public abstract class BaseActivity extends AppCompatActivity{

static DrawerLayout drawerLayout;
FrameLayout headerLayout;
LinearLayout homeLayout;
FragmentTransaction fragmentTransaction;
NavigationView navigationView;
Button btnOpenDrawer;
Boolean displayHeader= Boolean.TRUE;

protected abstract int getLayoutRes();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout_main);
    headerLayout= (FrameLayout) findViewById(R.id.header_layout);
    btnOpenDrawer= (Button) findViewById(R.id.btn_openDrawer);
    homeLayout= (LinearLayout) findViewById(R.id.home_layout);

    btnOpenDrawer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            drawerLayout.openDrawer(Gravity.LEFT);
        }
    });

    getNavigationControls();

    ViewGroup content = (ViewGroup) findViewById(R.id.base_layout);
    getLayoutInflater().inflate(getLayoutRes(),content,true);

  }

}

答案 1 :(得分:1)

尝试在Oncreate中使用此代码

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.activity_main, container, false);
            return rootView;
        }