如何在android中创建文本视图中心?
我正在设计一个动作栏,我设计了一个自定义动作栏,但该客户动作栏中的问题是我使用了一个文本视图,我需要它作为中心。 这里是我的自定义操作栏的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)));
你可以在你的XML尝试这与Relative Layout
<?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="50dp"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/title_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:layout_centerHorizontal="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="20sp"
android:text="@string/app_name"/>
</RelativeLayout>
完成但这不适用于我 – 2014-10-02 06:43:30
你可以添加你的应用程序的屏幕截图 – 2014-10-02 06:54:47
http://javatechig.com/android/actionbar-with-custom-view-example-in-android检查这个例子并使用它。 – 2014-10-02 06:56:37
试着改变你的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/title_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/app_name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#000000"
android:textSize="20sp" />
希望这会工作。
我也这样做过,但这不适合我 – 2014-10-02 06:37:46
我是新来的机器人,但我认为它的工作原理
你为什么要使用需要),而不是
1的relativeLayout
LinearLayout
<RelativeLayout 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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="@string/app_name"
android:textColor="#000000"
android:typeface="serif" />
</RelativeLayout>
对不起,这对我不适用 – 2014-10-02 06:42:33
@HiteshMatnani在'TextView'中使用'android:layout_width =“wrap_content”' – 2014-10-02 06:49:45
布局?为什么你没有在你的customactionbar.xml文件中有一个单独的TextView,其中android:gravity="center"
和宽度和高度设置为match_parent
?
尝试在你的TextView添加三个属性中删除任何其他重力属性
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
希望它可以帮助
我已经这样做了,但这个工作!如在图形布局它的表演完美,但在服装行动吧它中心属性不工作正确! – 2014-10-02 08:10:44
然后检查您的活动代码我认为这是您活动的问题 – Rajesh 2014-10-02 08:20:42
感谢您请检查我在上述问题中提及的活动代码! – 2014-10-02 08:56:34
你没有使用自定义的操作栏的默认 – 2014-10-02 06:28:27
使用相对布局,而不是线性布局 – 2014-10-02 06:28:29
添加此到你的xml''android:gravity =“center”'为'TextView' – prabhakaran 2014-10-02 06:43:16