从不起作用的普通活动扩展所有活动 [英] Extending all activities from a common activity not working

查看:62
本文介绍了从不起作用的普通活动扩展所有活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个application,其中有四个activities.所有这些活动应由顶部的toolbar和底部的TabLayout组成. TabLayout具有4个选项卡.单击特定的tab进入四个活动之一.

I have an application which has four activities. All these activities should consist of a toolbar at the top and a TabLayout at the bottom. The TabLayout has 4 tabs. Clicking on a particular tab takes to one of the four activities.

现在我要按以下方式实现代码

Now I want to implement the code as follows

1)创建一个父活动.该活动会放大包含TabLayout的布局.

1) Create a Parent activity. The activity inflates a layout which contains the TabLayout.

2)所有其他四个活动都应扩展父级活动.这些活动具有自己的xml布局.

2) All the other four activities should extend the Parent activity. These activities have their own xml layouts.

ParentActivity.java

    public class ParentActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_parent);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

  }
}

activity_parent.xml

activity_parent.xml

  <?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"
                xmlns:app="http://schemas.android.com/apk/res-auto">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="?attr/colorPrimary"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true">

        <android.support.design.widget.TabItem
            android:text="One"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>

        <android.support.design.widget.TabItem
            android:text="Two"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>

        <android.support.design.widget.TabItem
            android:text="Three"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>

        <android.support.design.widget.TabItem
            android:text="Four"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>

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

</RelativeLayout>

启动OneActivity时,无法获得在ParentActivity中创建的工具栏和布局.我只能得到在OneActivity的xml文件中声明的内容.

When I am launching OneActivity I am not able to get the toolbar and the tablayout which I created in the ParentActivity. I only get the content which I declared in the xml file of OneActivity.

我期望的ui是这个

这是我启动后得到的.

This is what I get after launching.

推荐答案

如果您在扩展的Activity中调用setContentView(),它将完全替换您在ParentActivity中设置和初始化的布局.

If you're calling setContentView() in the extended Activities, it's completely replacing the layout you set and initialize in ParentActivity.

相反,在ParentActivity的布局中包含内容ViewGroup-例如ID为contentFrameLayout-,然后将扩展的Activity的布局填充到其中.可以通过如下方式覆盖ParentActivity中的setContentView(int)方法来简单地完成此操作:

Instead, include a content ViewGroup - e.g., a FrameLayout with ID content - in ParentActivity's layout, and inflate the extended Activities' layouts into it. This can be done rather simply by overriding the setContentView(int) method in ParentActivity as follows:

@Override
public void setContentView(int layoutResID) {
    super.setContentView(R.layout.activity_parent);

    getLayoutInflater().inflate(layoutResID,
        (ViewGroup) findViewById(R.id.content));
}

您还可以将所有扩展活动通用的设置和初始化移至该替代中.

You can also move the setup and initialization common to all of the extended Activities into that override.

这篇关于从不起作用的普通活动扩展所有活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆