2016-09-16 45 views
1

我是Android编程的新手,所以我不知道所有功能。 据我所知,所有生成的活动都有一个标题栏,其中包含app_name字符串资源。我设法改变了唯一活动的标题,但我不知道如何改变标题文本的对齐方式。我的意思是,the're产生这样的:Android中标题栏文本的中心对齐

enter image description here

,但我需要的是这样的:

enter image description here

回答

2

如果您使用的是支持工具栏,该方法将非常简单。 只是其中加一个文本视图,使其中心:

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar_top" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:minHeight="?attr/actionBarSize" 
    android:background="@color/action_bar_bkgnd" 
    app:theme="@style/ToolBarTheme" > 


    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Toolbar Title" 
     android:layout_gravity="center" 
     android:id="@+id/toolbar_title" /> 


    </android.support.v7.widget.Toolbar> 
在您的活动

您可以访问标题像这样:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_top); 
TextView Title = (TextView) toolbar.findViewById(R.id.toolbar_title); 
0

要在ABS居中的标题(如果你想有这在默认的ActionBar中,只需删除方法名称中的“支持”),您可以这样做:

在您的活动中,在您的onCreate()方法中:

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.abs_layout); 

xml文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="my Title" 
     android:textColor="#ffffff" 
     android:id="@+id/mytext" 
     android:textSize="18sp" /> 

</LinearLayout> 

现在你应该有一个动作条只是一个标题。如果你想设置一个自定义背景,在上面的布局中设置它(但是不要忘记设置android:layout_height =“match_parent”)。