我正在設計一個動作欄,我設計了一個自定義動作欄,但該客戶動作欄中的問題是我使用了一個文本視圖,我需要它作爲中心。 這裏是我的自定義操作欄的XML代碼 -如何在android中創建文本視圖中心?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:gravity="center_horizontal"
android:layout_height="50dp"
>
<TextView
android:id="@+id/title_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="20sp"
android:text="@string/app_name"
/>
</LinearLayout>
這裏是使custome操作欄該活動我的Java代碼。
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(true);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.customactionbar, null);
TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#f8f8f8")));
Typeface fontt = Typeface.createFromAsset(getAssets(), "helvetica.ttf");
mTitleTextView.setTypeface(fontt);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
getActionBar().setIcon(
new ColorDrawable(getResources().getColor(android.R.color.transparent)));
你沒有使用自定義的操作欄的默認 – 2014-10-02 06:28:27
使用相對佈局,而不是線性佈局 – 2014-10-02 06:28:29
添加此到你的xml''android:gravity =「center」'爲'TextView' – prabhakaran 2014-10-02 06:43:16