2013-10-08 29 views
14

您好我正在开发Android应用程序。在我的应用程序中,我使用操作栏。我想让中心在行动栏中对齐我的窗口标题。我知道可以使用xml文件。但我想用风格去做,以便适用于我的每一个窗口,如何做到这一点需要帮助。谢谢。使用android中的样式中心对齐操作栏中的标题。

+0

我试过用xml工作,但我想用样式来做。因为对于每一个窗口我的标题将在中间。所以我想用样式来做。 – nilkash

+0

可以任何一个帮助http://stackoverflow.com/questions/31803463/how-to-set-action-bar-title-center-in-navigation-drawer – Aditya

回答

9

我知道这是可能使用XML文件。但我想用风格 ,使其适用于我的每一个窗口,如何做到这一点需要 帮助做到这一点。

仍然使用XML来做到这一点。只需将样式应用程序广泛应用于单个活动即可。请参阅Android文档完整地描述:

https://developer.android.com/training/basics/actionbar/styling.html

然后套用您的主题,你的整个应用程序或个别活动:

<application android:theme="@style/CustomActionBarTheme" ... /> 

编辑:下面是你必须处理标题的中心位置:

在action_bar.xml中,您将拥有如下所示的内容:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:text="SOME TITLE" 
    android:textSize="18sp" /> 
</RelativeLayout> 

然后在您的onCreate(),你会碰到这样的:

getSupportActionBar().setCustomView(R.layout.action_bar); 

如果你需要有这样的每一次活动,你有2种选择:

  1. 蛮力方式:将上面的代码行放在每个Activity的onCreate中。
  2. 扩展活动,把你的动作条初始化代码在OnCreate。然后为每个活动扩展您的自定义活动类。只要不要忘记在每个Activity的onCreate()中调用super()。
+1

谢谢你的帮助我检查了你给的链接。其实我可以设置标题的颜色大小,但不能做senter对齐。你对此有任何想法。需要帮忙。谢谢。 – nilkash

+0

更新了我的答案。希望它可以帮助你。 – SBerg413

+0

感谢您的帮助。我以同样的方式做了。但仍然觉得我不允许通过样式来对其进行中心化处理:( – nilkash

29

将ActionBarTitle居中而不使用自定义布局的简单方法。

但是,第一隐藏了图标为前:

myActionBar.setIcon(new ColorDrawable(Color.TRANSPARENT)); 

private void centerActionBarTitle() { 
    int titleId = 0; 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     titleId = getResources().getIdentifier("action_bar_title", "id", "android"); 
    } else { 
     // This is the id is from your app's generated R class when 
     // ActionBarActivity is used for SupportActionBar 
     titleId = R.id.action_bar_title; 
    } 

    // Final check for non-zero invalid id 
    if (titleId > 0) { 
     TextView titleTextView = (TextView) findViewById(titleId); 
     DisplayMetrics metrics = getResources().getDisplayMetrics(); 

     // Fetch layout parameters of titleTextView 
     // (LinearLayout.LayoutParams : Info from HierarchyViewer) 
     LinearLayout.LayoutParams txvPars = (LayoutParams) titleTextView.getLayoutParams(); 
     txvPars.gravity = Gravity.CENTER_HORIZONTAL; 
     txvPars.width = metrics.widthPixels; 
     titleTextView.setLayoutParams(txvPars); 
     titleTextView.setGravity(Gravity.CENTER); 
    } 
} 
+0

This works .. !! –

+2

重复与action_bar_subtitle相同的操作栏也会中心对齐 – Intrications

+3

Works,但如果ActionBar有MenuItems,则标题会移动 – saiyancoder

4

Unfortunally,SBerg413的方式不适合我的工作。

我的自定义布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/custom_action_bar_title" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:text="test" 
     android:textColor="@color/action_bar_title_color" 
     android:textSize="@dimen/action_bar_title_size" /> 
</RelativeLayout> 

和所有完美的作品:
http://i.stack.imgur.com/MvQgj.png

UPD:这个活动我喜欢用我所有的活动家长,应该有操作栏。

public class ActionBarCustomizationActivity extends ActionBarActivity 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     this.getSupportActionBar().setDisplayShowCustomEnabled(true); 

     LayoutInflater inflater = LayoutInflater.from(this); 
     View v = inflater.inflate(R.layout.custom_action_bar, null); 

     TextView titleTextView = (TextView) v.findViewById(R.id.custom_action_bar_title); 
     titleTextView.setText(this.getTitle()); 
     titleTextView.setTypeface(App.getInstance().getActionBarTypeFace()); 

     this.getSupportActionBar().setCustomView(v); 
    } 
} 
3

@拿破仑的答案帮了我,但在我的情况下,我使用的是Xamarin android(monodroid)。这里是他的代码在C#中的翻译:

var titleId = Resources.GetIdentifier("action_bar_title", "id", "android"); 
var titleTextView = FindViewById<TextView>(titleId); 
var layoutParams = (LinearLayout.LayoutParams) titleTextView.LayoutParameters; 
layoutParams.Gravity = GravityFlags.CenterHorizontal; 
layoutParams.Width = Resources.DisplayMetrics.WidthPixels; 
titleTextView.LayoutParameters = layoutParams; 
titleTextView.Gravity = GravityFlags.Center; 
//Added padding because it is slightly off centered. 
titleTextView.SetPadding(100, 0,0,0); 
+0

你从哪里调用这段代码?我已经从主要活动中完成了它,但是当OnConfigurationChange从旋转中触发时,重心会被重置。 – Ian