我有一个xml文件(fragment_about_sl),其中我添加了一个coordinatorLayout。下面给出了代码。添加并在设备上运行应用程序后,特定的布局显示了两个工具栏。我认为一个是在MainActivity.java和另一个是在coordinatorLayout中创建的空白工具栏。我使用了一个扩展到fragment.java的类作为fragment_about_sl.xml,扩展到的原因是因为我使用了navigationDrawer。
我想知道如何删除默认工具栏并将其替换为在coordinatorLayout中创建的工具栏。另外,当我运行应用程序时,在coordinatorLayout中创建的工具栏是空白的。我想要让所有内容都包含在内默认工具栏,它是汉堡图标,布局名称和新工具栏中的3个点设置。
fragment_about_sl
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.birjuvachhani.myapplication.ScrollingActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:fitsSystemWindows="true"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:titleEnabled="false"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:toolbarId="@+id/toolbar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_collapseMode="parallax">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="250dp" />
<LinearLayout
android:id="@+id/SliderDots"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/viewPager"
android:layout_marginBottom="20dp"
android:gravity="center_horizontal"
android:orientation="horizontal" />
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" >
<!-- Main content goes here. -->
</android.support.v4.widget.NestedScrollView>
aboutSLFragment.java
public class aboutSLFragment extends Fragment{
ViewPager viewPager;
LinearLayout sliderDotspanel;
private int dotscount;
private ImageView[] dots;
TextView aboutslintro;
Button readmorebtn1;
TextView aboutslhistory;
Button readmorebtn2;
TextView aboutslquickfacts;
Button readmorebtn3;
public aboutSLFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_about_sl, container, false);
viewPager = (ViewPager) rootView .findViewById(R.id.viewPager);
sliderDotspanel = (LinearLayout)rootView.findViewById(R.id.SliderDots);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getContext());
viewPager.setAdapter(viewPagerAdapter);
dotscount = viewPagerAdapter.getCount();
dots = new ImageView[dotscount];
for(int i = 0; i < dotscount; i++){
dots[i] = new ImageView(getContext());
dots[i].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.non_active_dot));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(8, 0, 8, 0);
sliderDotspanel.addView(dots[i], params);
}
dots[0].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.active_dot));
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
for(int i = 0; i< dotscount; i++){
dots[i].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.non_active_dot));
}
dots[position].setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.active_dot));
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
aboutslintro=(TextView)rootView.findViewById(R.id.aboutslintro);
readmorebtn1=(Button)rootView.findViewById(R.id.readmorebtn1);
readmorebtn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (readmorebtn1.getText().toString().equalsIgnoreCase("Read
More"))
{
aboutslintro.setMaxLines(Integer.MAX_VALUE);//your TextView
readmorebtn1.setText("Read Less");
}
else
{
aboutslintro.setMaxLines(5);//your TextView
readmorebtn1.setText("Read More");
}
}
});
aboutslhistory=(TextView)rootView.findViewById(R.id.aboutslhistory);
readmorebtn2=(Button)rootView.findViewById(R.id.readmorebtn2);
readmorebtn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (readmorebtn2.getText().toString().equalsIgnoreCase("Read
More"))
{
aboutslhistory.setMaxLines(Integer.MAX_VALUE);//your
TextView
readmorebtn2.setText("Read Less");
}
else
{
aboutslhistory.setMaxLines(5);//your TextView
readmorebtn2.setText("Read More");
}
}
});
aboutslquickfacts=
(TextView)rootView.findViewById(R.id.aboutslquickfacts);
readmorebtn3=(Button)rootView.findViewById(R.id.readmorebtn3);
readmorebtn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (readmorebtn3.getText().toString().equalsIgnoreCase("Read
More"))
{
aboutslquickfacts.setMaxLines(Integer.MAX_VALUE);//your
TextView
readmorebtn3.setText("Read Less");
}
else
{
aboutslquickfacts.setMaxLines(5);//your TextView
readmorebtn3.setText("Read More");
}
}
});
// Inflate the layout for this fragment
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//you can set the title for your toolbar here for different fragments
different titles
getActivity().setTitle("About Sri Lanka");
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)
findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
displaySelectedScreen(R.id.nav_home);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void displaySelectedScreen(int itemId) {
//creating fragment object
Fragment fragment = null;
//initializing the fragment object which is selected
switch (itemId) {
case R.id.nav_home:
fragment = new homeFragment();
break;
case R.id.nav_aboutSL:
fragment = new aboutSLFragment();
break;
case R.id.nav_topLandmarks:
fragment = new topLandmarksFragment();
break;
case R.id.nav_searchArea:
fragment = new searchAreaFragment();
break;
case R.id.nav_searchCat:
fragment = new searchCategoryFragment();
break;
case R.id.nav_events:
fragment = new eventsFragment();
break;
case R.id.nav_transport:
fragment = new transportFragment();
break;
case R.id.nav_commonPhrases:
fragment = new commonPhrasesFragment();
break;
case R.id.nav_utilities:
fragment = new utilitiesFragment();
break;
}
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
displaySelectedScreen(item.getItemId());
return true;
}
}
答案 0 :(得分:0)
如果要将工具栏包含在xml布局中,则必须将活动设置为无操作栏活动。 为此,请遵循简单的步骤。
with_entities
粘贴此行
<activity
android:name="yourActivity"
android:theme="@style/ThemeNoActionBar" />