您好我正在开发Android应用程序。在我的应用程序中,我使用操作栏。我想让中心在行动栏中对齐我的窗口标题。我知道可以使用xml文件。但我想用风格去做,以便适用于我的每一个窗口,如何做到这一点需要帮助。谢谢。使用android中的样式中心对齐操作栏中的标题。
回答
我知道这是可能使用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种选择:
- 蛮力方式:将上面的代码行放在每个Activity的onCreate中。
- 扩展活动,把你的动作条初始化代码在OnCreate。然后为每个活动扩展您的自定义活动类。只要不要忘记在每个Activity的onCreate()中调用super()。
将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);
}
}
This works .. !! –
重复与action_bar_subtitle相同的操作栏也会中心对齐 – Intrications
Works,但如果ActionBar有MenuItems,则标题会移动 – saiyancoder
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>
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);
}
}
@拿破仑的答案帮了我,但在我的情况下,我使用的是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);
你从哪里调用这段代码?我已经从主要活动中完成了它,但是当OnConfigurationChange从旋转中触发时,重心会被重置。 – Ian
- 1. 中心对齐钛操作栏中的图标为Android
- 2. 如何对齐操作栏标题在屏幕的中心,而不是操作栏的中心
- 3. Android中标题栏文本的中心对齐
- 4. android中的标题/操作栏ID?
- 5. 中心对齐标题中的JFrame
- 6. 如何对齐中心动作栏标题
- 7. 如何在Android上的动作栏中对齐中心项目
- 8. Android中的中心对齐
- 9. 工具栏中的标题图像对齐方式android
- 10. Android的自定义操作栏不填写父母,标题不居中对齐
- 11. ionic2:标题标题不对齐中心
- 12. 带有中心徽标的操作栏
- 13. 标题栏中的Android标题+按钮未正确对齐
- 14. 中心动作栏标题
- 15. 中心对齐分页栏
- 16. 的Android 4.2标题栏在操作栏
- 17. Android:在标题栏中更改对齐方式
- 18. 如何让android动作条标题中心对齐?
- 19. 中心对齐李的使用文本对齐:中心不起作用
- 20. 把android标签放在操作栏的中心
- 21. 的样式在Android操作栏,
- 22. 如何在Android中使用主题隐藏操作栏图标?
- 23. 主题android中的操作栏?
- 24. JQuery Mobile:切换栏中的标签中心文本对齐
- 25. 在android中的操作栏图标
- 26. Android:更改操作栏中的标签
- 27. dhtmlxlyout标题文本对齐中心
- 28. [R GGPLOT2中心对齐多行标题
- 29. 网格视图标题中心对齐
- 30. 将标题对齐到中心
我试过用xml工作,但我想用样式来做。因为对于每一个窗口我的标题将在中间。所以我想用样式来做。 – nilkash
可以任何一个帮助http://stackoverflow.com/questions/31803463/how-to-set-action-bar-title-center-in-navigation-drawer – Aditya