Java 折叠工具栏布局和工具栏操作按钮
如果有人能给我一些建议来解决我的最新问题,我将不胜感激 我有一个具有折叠工具栏布局的活动。在未折叠状态下,我无法让按钮工作。我不知道如何解决这个问题。在发布这篇文章之前,我已经搜索了stackoverflow,但没有找到任何有用的提示 我在这里发帖,希望得到答复 谢谢 这是我的密码Java 折叠工具栏布局和工具栏操作按钮,java,android,xml,Java,Android,Xml,如果有人能给我一些建议来解决我的最新问题,我将不胜感激 我有一个具有折叠工具栏布局的活动。在未折叠状态下,我无法让按钮工作。我不知道如何解决这个问题。在发布这篇文章之前,我已经搜索了stackoverflow,但没有找到任何有用的提示 我在这里发帖,希望得到答复 谢谢 这是我的密码 <?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:andro
<?xml version="1.0" encoding="utf-8"?>
<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:background="?attr/app_background"
android:fitsSystemWindows="true"
tools:context="com.company.walt.activities.photos.PhotosAAAActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/backdrop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/ip_photo_header"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/love_music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AAAAAA"
android:textColor="@android:color/white"
android:textSize="@dimen/ssi_txt_40sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BBBBBBBB"
android:textColor="@android:color/white"
android:textSize="@dimen/ssi_txt_20sp" />
</LinearLayout>
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="@+id/main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="@dimen/ssi_24dp"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:tabIndicatorColor="@color/white"
app:tabSelectedTextColor="@color/white"
app:tabTextColor="@color/white" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.CoordinatorLayout>
更新!-2018-01-13
我发现了问题的症结所在,但我仍然不知道如何解决这个问题
问题是,如果我删除了这个代码,那么标签就不会出现,所以我感觉就像这样
问题
//region Used to create the tab layout
private void setViewPager() {
ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
mPhotosPagerAdapter = new PhotosPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mPhotosPagerAdapter);
mTabLayout = (TabLayout) findViewById(R.id.tab);
mTabLayout.setupWithViewPager(mViewPager);
}
//endregion
单击listener上的后退按钮
单击listener上的后退按钮 试试这个
在你的活动中
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
在xml中
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
//for menu you should give ids to that menu items in your menu file
//R.menu.photo_category and after that if you have 2 menu items
// you have to write listener for every single items for onClick like.
case R.id.item1:
finish();
break;
case R.id.item2:
finish();
break;
}
return true
}试试这个
在你的活动中
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setHomeButtonEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
}
在xml中
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
//for menu you should give ids to that menu items in your menu file
//R.menu.photo_category and after that if you have 2 menu items
// you have to write listener for every single items for onClick like.
case R.id.item1:
finish();
break;
case R.id.item2:
finish();
break;
}
return true
}您的代码有两个问题,都与您尝试使用的按钮有关 首先,使用home按钮或up按钮,您需要在使用actionBar处理事件之前使用actionBar激活它。添加以下内容:
getSupportActionBar().setHomeButtonEnabled(true);
在setupToolbar方法中。现在,您可以使用您在案例android.R.id.home:下编写的代码访问该按钮
第二个按钮也有同样的问题。您尚未添加任何逻辑来处理按钮单击。假设您右侧的按钮具有id=@+id/更多。现在要为它定义一个动作,需要将一个id为的case放在switch中
case R.id.more:
//The action needed for the button
break;
您的代码有两个问题,都与您尝试使用的按钮有关 首先,使用home按钮或up按钮,您需要在使用actionBar处理事件之前使用actionBar激活它。添加以下内容:
getSupportActionBar().setHomeButtonEnabled(true);
在setupToolbar方法中。现在,您可以使用您在案例android.R.id.home:下编写的代码访问该按钮
第二个按钮也有同样的问题。您尚未添加任何逻辑来处理按钮单击。假设您右侧的按钮具有id=@+id/更多。现在要为它定义一个动作,需要将一个id为的case放在switch中
case R.id.more:
//The action needed for the button
break;
您调试过了吗,是否使用clickListener?您调试过了吗,是否使用clickListener?尝试此操作后,它不起作用。你有更多的提示吗?尽管如此,它不起作用。你有更多的小费吗