0

タイトル バーに画像とテキストを表示するコードを作成しました。テキストは表示されますが、画像は表示されません。

これがlayout_my_title.xmlです

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/ic_launcher"
    android:padding="5dp"
    android:text="@string/bv_title_string"
    android:textColor="@color/White"
    android:textSize="20sp" />

ここにstyles.xmlがあります

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="customTitleBackground">
            <item name="android:background">@color/titlebackgroundcolor</item>
    </style>
</resources>

ここにthemes.xmlがあります

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="customTheme" parent="android:Theme">
        <item name="android:windowTitleSize">45dp</item>
        <item name="android:windowTitleBackgroundStyle">@style/customTitleBackground</item>
    </style>

4

1 に答える 1

0
  • 「layout」フォルダーにウィンドウ タイトルのカスタム レイアウトを作成します。

window_title.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="horizontal"
     android:layout_width="fill_parent"
    android:layout_height="35dip"
    android:gravity="center_vertical"
    android:paddingLeft="5dip"
     android:background="#323331"> 
<ImageView
    android:id="@+id/header"
    android:src="@drawable/header"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</LinearLayout>
  • 「values」フォルダにカスタム スタイルを作成します。

custom_style.xml

  <resources>        
  <style name="CustomWindowTitleBackground">
    <item name="android:background">#323331</item>
  </style>
  <style name="CustomTheme" parent="android:Theme">
    <item name="android:windowTitleSize">35dip</item>
    <item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>

  • カスタム スタイルをマニフェスト ファイルにテーマとして適用します。 AndroidManifest.xml
<application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/CustomTheme">
  • メイン アクティビティ クラスにカスタム ウィンドウ タイトルを適用する CustomWindowTitle.xml
public class CustomWindowTitle extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    setContentView(R.layout.main); 
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
}   }

これがあなたを助けることを願っています。

于 2013-01-01T12:09:20.130 に答える