简体   繁体   English

自定义ActionBar未显示

[英]Custom ActionBar is not showing

I'm beginner in android development.. I don't know where is the problem.. 我是android开发的初学者..我不知道问题出在哪里..

The activity does not show custom actionbar... i tried it by watching a tutorial.. pls give some solution.. thanks in advance.. 活动没有显示自定义操作栏...我通过观看教程尝试了..请给出一些解决方案..提前感谢..

actoin_bar.xml actoin_bar.xml

` `

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="36dp"
    android:background="#009688" >

    <TextView
        android:id="@+id/title_text_small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff"
        android:textStyle="bold" />


    <ImageView
        android:id="@+id/forward"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:src="@drawable/ic_action_name"
        android:layout_margin="8dp"
        android:layout_toLeftOf="@+id/refresh"
        android:layout_toStartOf="@+id/refresh"
        android:contentDescription="@string/tabb" />
    <ImageView
        android:id="@+id/backward"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:layout_margin="8dp"
        android:src="@drawable/ic_action_backward"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/forward"
        android:layout_toStartOf="@+id/forward"
         />

    <ImageView
        android:id="@+id/refresh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        android:src="@drawable/ic_action_reload"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_marginTop="6dp"
        android:layout_marginBottom="6dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="16dp" />

</RelativeLayout>

` `

subtitle.java subtitle.java

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Subtitle extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_subtitle);



        ActionBar mActionBar;
        mActionBar = getSupportActionBar();
        if (mActionBar != null) {

            getSupportActionBar().setCustomView(R.layout.action_bar);
            getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        }


        TextView mTitleTextView = null;
        if (mActionBar != null) {

            mTitleTextView = (TextView) mActionBar.getCustomView().findViewById(R.id.title_text_small);
        }
        if (mTitleTextView != null) {
            mTitleTextView.setText(R.string.title);
        }

        ImageView reloadButton = null;
        if (mActionBar != null) {
            reloadButton = (ImageView) mActionBar.getCustomView().findViewById(R.id.refresh);
        }
        if (reloadButton != null) {
            reloadButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    mWebView.reload();
                    Toast.makeText(getApplicationContext(), "Refresh",
                            Toast.LENGTH_LONG).show();
                }
            });
        }


        ImageView backButton = null;
        if (mActionBar != null) {
            backButton = (ImageView) mActionBar.getCustomView().findViewById(R.id.backward);
        }
        if (backButton != null) {
            backButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (mWebView.canGoBack()) {
                        mWebView.goBack();
                    }
                }
            });
        }


        ImageView forButton = null;
        if (mActionBar != null) {
            forButton = (ImageView) mActionBar.getCustomView().findViewById(R.id.forward);
        }
        if (forButton != null) {
            forButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    if (mWebView.canGoForward()) {
                        mWebView.goForward();
                    }
                }
            });
        }



    }

        @Override
        public void onBackPressed () {
            if (mWebView.canGoBack()) {
                mWebView.goBack();
            } else {
                super.onBackPressed();
            }
        }


    }

First create an inflater and custom view like below: 首先创建一个如下所示的inflater和自定义视图:

LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.your_custom_actionbar_xml, null);

TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text_small);
mTitleTextView.setText("Bla bla bla);

ImageView img = (ImageView) mCustomView.findViewById(R.id.forward);
//.... more view

Then set custom view for action bar: 然后为操作栏设置自定义视图:

mActionBar.setCustomView(mCustomView);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM