自定义ActionBar未显示

时间:2016-08-02 20:26:19

标签: java android

我是android开发的初学者.. 我不知道问题出在哪里..

活动未显示自定义操作栏... 我通过观看教程试了一下.. 请给出一些解决方案.. 提前谢谢..

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

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();
            }
        }


    }

1 个答案:

答案 0 :(得分:0)

首先创建一个如下所示的inflater和自定义视图:

$.getJSON

然后设置操作栏的自定义视图:

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