您好我正在開發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