18

我想在我的应用程序中显示后退箭头按钮,但是当我将它放入我的代码时应用程序崩溃:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

样式.xml

 <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/PrimaryColor</item>
    <item name="colorPrimaryDark">@color/PrimaryColorDark</item>
    <item name="colorAccent">@color/PrimaryColor</item>
    <item name="colorControlHighlight">@color/ControlHighlight</item>
    <item name="android:statusBarColor" tools:targetApi="21">@android:color/transparent</item>
</style>


<style name="ActionBarPopupThemeOverlay" parent="ThemeOverlay.AppCompat.Light" >
    <item name="android:textColor">#000000</item>
</style>


<style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
    <item name="tabIndicatorColor">#FFFFFF</item>
    <item name="tabIndicatorHeight">3dp</item>
    <item name="tabTextAppearance">@style/MyCustomTabTextAppearance</item>
</style>


<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
    <item name="android:textColor">#FFFFFF</item>
</style>

工具栏.xml

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="@dimen/abc_action_bar_default_height_material"
android:elevation="0dp"
android:layout_width="match_parent"
android:id="@+id/toolbar"
android:background="@color/PrimaryColor"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ActionBarPopupThemeOverlay"/>

类(不是片段)

public class WebViewCanale extends AppCompatActivity {

    Toolbar toolbar;
    TextView textView;
    WebView webView;
    ProgressBar progressBar;

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

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        //Toolbar
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        //FindViewById
        webView = (WebView) findViewById(R.id.webview);
        textView = (TextView) findViewById(R.id.textView);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);

        //Impostazioni WebView
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setLoadsImagesAutomatically(true);
        webView.loadUrl(this.getIntent().getDataString());
        webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        webView.getSettings().setAppCacheEnabled(false);

        //Titolo Toolbar
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                if (!TextUtils.isEmpty(title)) {
                    WebViewCanale.this.setTitle(title);
                }
            }
        });
    }
}
4

5 回答 5

31

您也可以在ManifestaParentActivity中声明: <activity android:name=".ThisActivity" android:parentActivityName=".TheActivityThatCalledThisOne"> </activity>

于 2016-08-21T11:54:52.850 回答
28

我想知道为什么在 SO 上没有完整的答案,但最后我自己解决了这个问题:

使用以下代码片段在AppCompatActivity工具栏上有返回按钮

        toolbar = (Toolbar) findViewById(R.id.hack_toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //do something you want
            }
        });
于 2015-12-12T05:38:22.950 回答
8

更新

      private lateinit var toolbar: Toolbar
    
      override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = DataBindingUtil.setContentView<DetailActivityBinding>(
          activity,
          R.layout.detail_activity
        )
        toolbar = binding.toolbar
        activity.setSupportActionBar(toolbar)
        activity.supportActionBar!!.setDisplayShowHomeEnabled(true)
        activity.supportActionBar!!.setDisplayHomeAsUpEnabled(true)
      }

原来的

 @BindView(R.id.toolbar)
 Toolbar toolbar;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_events);
    ButterKnife.bind(this);

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 }

 @Override
 public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
 }

onSupportNavigateUp() 每当用户从操作栏中选择在应用程序的活动层次结构中向上导航时,都会调用此方法。

onBackPressed(); 注意将片段弹回堆栈或酌情完成活动。

于 2017-07-10T08:31:45.880 回答
3

您需要先设置支持操作栏,然后才能获得它。所以放置以下代码

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

本守则之前

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
于 2015-09-07T08:37:12.863 回答
3

添加这个主题:

<style name="AppTheme2" parent="Theme.AppCompat.Light">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="colorPrimary">@color/appColor</item>
    <item name="colorControlNormal">@color/whiteColor</item>
    <item name="colorControlActivated">@color/appColor</item>
</style>

在你的活动中:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
于 2015-09-07T07:51:17.537 回答