gpt4 book ai didi

java - 尽管设置了属性,Android 工具栏标题、副标题和后退按钮仍不显示

转载 作者:行者123 更新时间:2023-12-01 08:53:29 25 4
gpt4 key购买 nike

创建工具栏来替换操作栏后,工具栏未产生预期结果。工具栏本身根据需要以指定的颜色显示,但由于某种原因,标题、副标题和后退按钮根本不显示。该 Activity 的主题甚至已设置为 Theme.AppCompat.NoActionBar

layout/toolbar_turquoise.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar_turquoise"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/turquoise"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

TurquoiseActivity.java

public class TurquoiseActivity extends AppCompatActivity {

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

ActionBar actionBar = getSupportActionBar();
if(actionBar != null) {
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_turquoise);
setSupportActionBar(myToolbar);

myToolbar.setTitle("Hello World");
myToolbar.setTitleTextColor(Color.parseColor("#000099"));
myToolbar.setSubtitle("Lorem ipsum dolor sit amet");
myToolbar.setSubtitleTextColor(Color.parseColor("#000099"));

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_material);
upArrow.setColorFilter(Color.parseColor("#000099"), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
}

FragmentTurquoise newFragment = new FragmentTurquoise();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.master_container, newFragment);
transaction.commit();
}
}

最佳答案

试试这个,

XML 文件:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/turquoise"
android:minHeight="100dp">

</android.support.v7.widget.Toolbar>

</RelativeLayout>

Java 类:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
if(toolbar != null) {
setSupportActionBar(toolbar);

toolbar.setTitle("Hello World");
toolbar.setTitleTextColor(Color.parseColor("#000099"));
toolbar.setSubtitle("Lorem ipsum dolor sit amet");
toolbar.setSubtitleTextColor(Color.parseColor("#000099"));
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

final Drawable upArrow = ContextCompat.getDrawable(this, R.drawable.abc_ic_ab_back_material);
upArrow.setColorFilter(Color.parseColor("#000099"), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
}

关于java - 尽管设置了属性,Android 工具栏标题、副标题和后退按钮仍不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42213825/

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