【问题标题】:Can't get activity contents under navigational drawer menu's toolbar无法在导航抽屉菜单工具栏下获取活动内容
【发布时间】:2017-06-12 11:04:14
【问题描述】:

我正在尝试实现与每个活动的工具栏一起出现的导航抽屉菜单。我决定创建通用的 menuactivity 类,所有其他活动都从它扩展而来。它工作正常,只是它将活动放在导航工具栏下。菜单。

MenuActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
...
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {
            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                changeActivity();
            }
        };
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setItemIconTintList(null);

        navigationView.getMenu().getItem(currentMenuItem).setChecked(true);
...

在同一个班级我管理项目的触摸(菜单项选择):

Intent intent = new Intent(getApplicationContext(), TrainingActivity.class);
startActivity(intent);

在 TrainingActivity 中:

public class TrainingActivity extends MenuActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.setStartingState(MenuItemNames.TRAINING);
        super.onCreate(savedInstanceState);
        FrameLayout contentLayout = (FrameLayout) findViewById(R.id.content_frame);
        View contentView = getLayoutInflater().inflate(R.layout.activity_training, contentLayout, false);
        drawer.addView(contentView, 0);

//        setContentView(R.layout.activity_training);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

...

这里是activity_menu.xml:

<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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <include
            layout="@layout/app_bar_menu"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/content_frame"/>
    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"

        app:menu="@menu/activity_menu_drawer" />

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

app_bar_menu.xml:

<android.support.design.widget.CoordinatorLayout 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:id="@+id/app_bar_menu_coord_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.magnifi.pennantrace.MenuActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/relativelayout"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <TextView
                    android:layout_width="wrap_content"
                    android:textStyle="bold"
                    android:layout_height="wrap_content"
                    android:id="@+id/toolbar_div_rank"
                    android:text="4th Place"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent" />

                ...

            </android.support.constraint.ConstraintLayout>
        </android.support.v7.widget.Toolbar>

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

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

所以我试图实现的逻辑是,每当在菜单中选择项目时,我都会将适当的 Activity(膨胀)加载到 FrameLayout(activity_menu.xml 中工具栏下的 content_frame)中。所以似乎它必须在工具栏下,但事实并非如此。你能帮我告诉我我的错误在哪里吗?

【问题讨论】:

  • drawer.addView(contentView, 0); - 您想将contentView 添加到contentLayout,而不是drawer
  • @MikeM。如果我执行 contentLayout.addView(contentView, 0);那么它不会显示任何空白,除了菜单/工具栏。
  • 是的,刚刚注意到,你在垂直的LinearLayout 中有两个东西,它持有content_frame,并且都有match_parent 的高度,所以content_frame 被推出了底部。您可能想将content_frame 移动到app_bar_menu,然后去掉DrawerLayout 中的封闭LinearLayout
  • @MikeM。试过了,没用。 (没有区别)
  • 嗯,你需要知道你在用CoordinatorLayout做什么。有时,这很棘手。您确定需要CoordinatorLayout 吗?在任何情况下,如果您在 XML 中将 FrameLayout 放在 AppBarLayout 下方,则可以将其添加到其中:stackoverflow.com/a/33229375

标签: android android-layout android-activity android-toolbar


【解决方案1】:

我通过用片段替换活动解决了这个问题。 因此,不要将活动声明为: public class TrainingActivity extends MenuActivity {

我将其替换为: public class TrainingActivity extends Fragment {

活动示例更改为片段:

public class TrainingActivity extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public static TrainingActivity newInstance(String param1, String param2) {
        TrainingActivity fragment = new TrainingActivity();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.activity_training, container, false);
        imageButtonsSetup(view);
        return view;
    }

    private void imageButtonsSetup(View view) {
        setImageButtonWithResourceId(view, R.id.imageButtonSelectTraining);
        setImageButtonWithResourceId(view, R.id.imageButtonSelectPlayers);
    }

    private void setImageButtonWithResourceId(View view, int resourceId) {
        ((ImageButton)view.findViewById(resourceId)).setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                        ImageButton view = (ImageButton ) v;
                        view.getBackground().setColorFilter(0x77777700, PorterDuff.Mode.SRC_ATOP);
                        v.invalidate();
                        break;
                    }
                    case MotionEvent.ACTION_UP:

                        // Your action here on button click

                    case MotionEvent.ACTION_CANCEL: {
                        ImageButton view = (ImageButton) v;
                        view.getBackground().clearColorFilter();
                        view.invalidate();
                        break;
                    }
                }
                return true;
            }
        });
    }
}

如果选择的菜单项发生了变化,我会执行以下操作:

try {
                fragment = (Fragment) fragmentClass.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

所以我不再需要抽屉 addView 等。因为我现在正在替换 content_frame。

最终的 app_bar_menu.xml 改成这样:

<LinearLayout 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:id="@+id/app_bar_menu_coord_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    tools:context="com....">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            .....
        </android.support.v7.widget.Toolbar>
    </android.support.design.widget.AppBarLayout>

    <FrameLayout
        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"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/content_frame"/>
</LinearLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多