簡體   English   中英

Android自定義操作欄

[英]Android custom action bar

我有一個問題,就是我在應用程序中添加了自定義操作欄,但是操作欄視圖中有一個間隙(鏈接圖片中的紅色圓圈位置)。

在此處輸入圖片說明

MainActivity代碼:

public class MainActivity extends ActionBarActivity {

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

        getSupportActionBar().setCustomView(R.layout.actionbar_layout);
        getSupportActionBar().setDisplayShowCustomEnabled(true);

    }
}

actionbar_layout代碼:

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="#0146A3">
    <TextView
        android:id="@+id/abr_title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:singleLine="true"
        android:textColor="#FFFFFF"
        android:textSize="26sp"
        android:text="Title"
        tools:text="Title"/>
</RelativeLayout>

但不使用android支持庫,則不會發生此問題。(“ MainActivity extands ActionBarActivity”更改為“ MainActivity extands Activity”,而“ getSupportActionBar()更改為getActionBar()”)

使用style.xml設置操作欄背景

<style name="AppTheme" parent="AppBaseTheme">
        <item name="android:actionBarItemBackground">@drawable/bg_actionbar_selected</item>

</style>


<style name="action_bar" parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/yourBackgound or color</item>
</style>

讓我建議另一種方法:步驟1:隱藏操作欄。 使用此主題進行活動。

 android:theme="@android:style/Theme.NoTitleBar"

步驟2:使用自定義相對布局作為操作欄

    <RelativeLayout
                    android:layout_width="fill_parent"
                    android:layout_height="60dp"
                    android:background="#009ACD">
           </RelativeLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM