gpt4 book ai didi

android - 如何在工具栏上绘制带有抽屉导航和自定义图标的 Material 滑动标签?

转载 作者:行者123 更新时间:2023-11-30 01:19:11 25 4
gpt4 key购买 nike

enter image description here

我想要一个完全像这样的布局。我已经搜索了很多相关内容并尝试了以下链接。

这是我到目前为止尝试过的代码。

MainActivity

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.astuetz.PagerSlidingTabStrip;

public class MainActivity extends AppCompatActivity {

NavigationView navigationView;
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar mToolbar;


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

mToolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);

navigationView = (NavigationView) findViewById(R.id.nvView);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

ViewPager pager = (ViewPager) findViewById(R.id.pager);
if (pager != null) {
pager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager()));
}

// Bind the tabs to the ViewPager
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
if (tabs != null) {
if (pager != null) {
tabs.setViewPager(pager);
}
}



navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {

if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);

drawerLayout.closeDrawers();

switch (menuItem.getItemId()) {

case R.id.home:

Toast.makeText(getApplicationContext(), "Home Selected", Toast.LENGTH_SHORT).show();

return true;

case R.id.about:


Toast.makeText(getApplicationContext(), "About Us Selected", Toast.LENGTH_SHORT).show();
return true;

case R.id.Share:


Toast.makeText(getApplicationContext(), "Share Selected", Toast.LENGTH_SHORT).show();
return true;

default:

Toast.makeText(getApplicationContext(), "Somethings Wrong", Toast.LENGTH_SHORT).show();
return true;

}
}
});



actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close) {

@Override
public void onDrawerClosed(View drawerView) {

super.onDrawerClosed(drawerView);
}

@Override
public void onDrawerOpened(View drawerView) {

super.onDrawerOpened(drawerView);

}
};
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {
case android.R.id.home:
int id = item.getItemId();

if (drawerLayout.isDrawerOpen(navigationView))
{
drawerLayout.closeDrawer(navigationView);
}
else
{
drawerLayout.openDrawer(navigationView);
}

}
return super.onOptionsItemSelected(item);

}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}





public static class SampleFragmentPagerAdapter extends FragmentPagerAdapter{


final int PAGE_COUNT = 3;
private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" };

public SampleFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public int getCount() {
return PAGE_COUNT;
}

@Override
public Fragment getItem(int position) {
return PageFragment.newInstance(position + 1);
}

@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}

public static class PageFragment extends Fragment {
private int mPage;
public static final String ARG_PAGE = "ARG_PAGE";

public static PageFragment newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
PageFragment fragment = new PageFragment();
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPage = getArguments().getInt(ARG_PAGE);
}

// Inflate the fragment layout we defined above for this fragment
// Set the associated text for the title
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.custom, container, false);
TextView tvTitle = (TextView) view.findViewById(R.id.text);
tvTitle.setText("Fragment #" + mPage);
return view;
}


}
}
}

activity_main

<?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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusableInTouchMode="true">

<android.support.design.widget.CoordinatorLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/overview_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
app:layout_scrollFlags="enterAlways|scroll" />

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

<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
/>

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tabs"
tools:context=".MainActivity" />

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

<android.support.design.widget.NavigationView
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
app:menu="@menu/drawer" />

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

使用这段代码,我只是得到了没有导航图标的抽屉导航,并且没有用它实现 viewpager。

最佳答案

关于android - 如何在工具栏上绘制带有抽屉导航和自定义图标的 Material 滑动标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37375588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com