gpt4 book ai didi

java - 自定义操作栏未显示

转载 作者:太空宇宙 更新时间:2023-11-04 12:22:15 25 4
gpt4 key购买 nike

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

该 Activity 不显示自定义操作栏...我通过观看教程尝试了一下。请给出一些解决方案..提前致谢..

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>

`

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


}

最佳答案

首先创建一个充气器和自定义 View ,如下所示:

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

然后为操作栏设置自定义 View :

mActionBar.setCustomView(mCustomView);

关于java - 自定义操作栏未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38729848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com