2015-10-12 184 views
0

我創建了一個自定義操作欄對齊自定義操作欄

ActionBar actionBar = getActivity().getActionBar(); 
    actionBar.setCustomView(R.layout.action_bar_draft); 
    actionBar.setDisplayShowCustomEnabled(true); 
    actionbarFeed = (TextView) actionBar.getCustomView().findViewById(R.id.textView_blogtest); 
    actionbarFeed.setText("My Photos and videos"); 

問題是文本視圖不對齊中心

enter image description here

在我的佈局文本視圖爲中心。當它來到行動吧時,它被轉移到右側。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 
<RelativeLayout 
    android:id="@+id/relativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" > 

    <TextView 
     android:id="@+id/textView_blogtest" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="Medium Text" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="#000" /> 
</RelativeLayout> 

<style name="AppTheme.ActionBarStyle" parent="@style/Widget.Sherlock.ActionBar.Solid"> 
    <item name="android:icon">@drawable/menu</item> 
    <item name="android:background">#ffca36</item> 
    <item name="background">#f66605</item> 
    <item name="android:customNavigationLayout">@layout/action_bar</item> 
</style> 
+0

您可以發佈您的自定義XML佈局嗎? –

+0

是的。它解決了 – DKV

回答

0

尋找一個答案在這裏:How to center align the ActionBar title in Android?

基本上你應該改變你的xml文件的操作欄。

編輯:

發表您的xml文件的操作欄以獲得更準確的答案...

EDIT2:
我不知道它。但提示調試:將textview的背景顏色設置爲其他顏色並張貼其截圖。由此您可以看到問題是textview的內容是錯誤的,還是整個textview未正確放置。

也許您只需要在你的TextView

android:gravity="center" 

0

,請使用以下功能集動作條

public void setMainScreenActionBar(String p_title) 
{ 

     LayoutInflater m_inflator = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     ActionBar m_actionBar = getSupportActionBar(); 
     View m_viewActionBar = m_inflator.inflate(R.layout.custom_actionbar_title_layout, null); 
     ActionBar.LayoutParams m_params = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER); 
     TextView m_tvTitle = (TextView) m_viewActionBar.findViewById(R.id.title); 
     m_tvTitle.setText(p_title); 
     m_actionBar.setCustomView(m_viewActionBar, m_params); 
     m_actionBar.setDisplayShowCustomEnabled(true); 
     m_actionBar.setDisplayShowTitleEnabled(false); 
     m_actionBar.setDisplayHomeAsUpEnabled(true); 
     m_actionBar.setHomeButtonEnabled(true); 
} 

custom_actionbar_title_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:paddingTop="10dp" 
     android:textColor="@android:color/black" 
     android:textSize="18sp" 
     android:textStyle="bold" /> 

</LinearLayout> 

寫下面的代碼在你的style.xml

<resources> 

    <style name="AppTheme" parent="Theme.AppCompat.Light"> 
     <item name="actionBarStyle">@style/MyActionBar</item> 
     <item name="android:actionBarStyle">@style/MyActionBar</item> 
     <!-- Customize your theme here. --> 
    </style> 


    <style name="MyCustomTheme" parent="@style/Theme.AppCompat.Light"> 
     <item name="actionBarStyle">@style/MyActionBar</item> 
     <item name="android:actionBarStyle">@style/MyActionBar</item> 
    </style> 
    <!-- ActionBar styles --> 
    <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse"> 
     <item name="android:background">@color/orange</item> 
     <item name="background">@color/orange</item> 
     <item name="android:homeAsUpIndicator">@drawable/ic_backarrow</item> 
     <item name="homeAsUpIndicator">@drawable/ic_backarrow</item> 
    </style> 

</resources> 
+0

它會隱藏我的導航抽屜圖標。 – DKV

0

在你onCreate()方法:

actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
LayoutInflater mInflater = LayoutInflater.from(context); 
actionBar.setDisplayHomeAsUpEnabled(true); 
actionBar.setCustomView(mInflater.inflate(R.layout.custom_layout, null), 
    new ActionBar.LayoutParams(
     ActionBar.LayoutParams.WRAP_CONTENT, 
     ActionBar.LayoutParams.MATCH_PARENT, 
     Gravity.CENTER 
)); 

custom_layout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Your title" 
     android:textColor="#ffffff" 
     android:id="@+id/mytext" 
     android:textSize="14dp" /> 

</LinearLayout> 

然後,你將有一個Actionbar只有一個標題。

+0

這將隱藏我的左側導航抽屜圖標 – DKV

+0

我編輯了我的答案 –

+0

在操作蝙蝠發生的轉變是由於菜單圖標。我的風格有什麼問題嗎? – DKV