2015-10-12 101 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