我需要导航到之前的活动。我想使用操作栏的箭头(工具栏)。我无法在操作栏(工具栏)中看到向上箭头。我附上下面的活动代码。
ImageActivity.java
public class ImageActivity extends AppCompatActivity {
private Toolbar toolbar;
int img,img1;
String text;
ImageView imgicon;
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().show();
Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(ContextCompat.getColor(this, R.color.white), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
getSupportActionBar().setHomeButtonEnabled(true);
imgicon = (ImageView) findViewById(R.id.img);
Bundle bundle = getIntent().getExtras();
if(bundle!=null)
{
img = bundle.getInt("img");
text = bundle.getString("Phone");
getSupportActionBar().setTitle("text");
imgicon.setImageResource(img);
}
img1 = bundle.getInt("img1");
imgicon.setImageResource(img1);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//Write your logic here
this.finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
有没有人遇到过这种情况,并以某种方式找到了解决问题的方法?提前谢谢。
答案 0 :(得分:0)
请尝试使用此
android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
try {
setSupportActionBar(toolbar);
ActionBar actionbar = getSupportActionBar();
if (actionbar != null) {
actionbar.setHomeButtonEnabled(true);
actionbar.setDisplayShowHomeEnabled(true);
actionbar.setDisplayHomeAsUpEnabled(true);
}
并在XML中使用它
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorWhite"
app:popupTheme="@style/AppTheme.PopupOverlay" />
答案 1 :(得分:0)
我的解决方案:
// Show the Up button in the action bar.
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}