我知道设置activity标题有两种方式。一种是在Android清单文件中设置,像这样android:label="@string/app_name"。第二种是在activity类中以编程方式设置,如setTitle("Hello World!")。这两种方式都位于左侧,但我该如何将它们放置在中心位置呢?
我知道设置activity标题有两种方式。一种是在Android清单文件中设置,像这样android:label="@string/app_name"。第二种是在activity类中以编程方式设置,如setTitle("Hello World!")。这两种方式都位于左侧,但我该如何将它们放置在中心位置呢?
它会正常工作。
TextView customView = (TextView)
LayoutInflater.from(this).inflate(R.layout.actionbar_custom_title_view_centered,
null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER );
customView.setText("Some centered text");
getSupportActionBar().setCustomView(customView, params);
((TextView)((FrameLayout)((LinearLayout)((ViewGroup) getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER);
Caused by: java.lang.ClassCastException: com.android.internal.widget.ActionBarView cannot be cast to android.widget.TextView
。 - VikramVprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // must come before setContentView
setContentView(R.layout.activity_calculator);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myTitle"
android:text="Custom Centered Title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@android:color/black"
android:gravity="center"
/>
您需要定义一个自定义的标题样式/主题。在示例中查找com.example.android.apis.app.CustomTitle。