【发布时间】:2016-02-16 04:39:14
【问题描述】:
我正在尝试自定义我的 android 应用程序中的标题栏。为了做到这一点,我使用了this问题的公认答案。
这是我使用的代码。
这是我的titlebar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400dp"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView android:id="@+id/ImageView01"
android:layout_width="57dp"
android:layout_height="wrap_content"
android:background="@drawable/ic_prof"/>
<TextView
android:id="@+id/myTitle"
android:text="This is my new title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="#FFA500"
/>
</LinearLayout>
这是我的main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
这是我的TitleBar.java
package com.myayubo;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
public class TitleBar extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if (myTitleText != null) {
myTitleText.setText("NEW TITLE");
// user can also set color using "Color" and then
// "Color value constant"
myTitleText.setBackgroundColor(Color.GREEN);
}
}
}
这是我的strings.xml
<string name="hello_world">Hello world!</string>
<string name="app_name">My Ayubo</string>
<color name="titlebackgroundcolor">#3232CD</color>
<color name="titletextcolor">#FFFF00</color>
但是,我无法使用以下代码自定义标题栏。我的意思是,代码运行没有任何错误。但是,它并没有达到我的预期。我需要在我的标题中添加图像和标题。我该怎么做?
【问题讨论】:
-
Google 不再需要 ActionBar。检查此链接blog.xamarin.com/android-tips-hello-toolbar-goodbye-action-bar
-
这里有一些提示,使用 AppCompatActivity。将主题与 NoActionBar 一起使用。并在你的 layout.xml 中使用 Toolbar
-
我的应用程序的最低 SDK 版本是 15。所以,在这种情况下,我不能使用 Toolbar 来代替这个,ryt?