Java 如何将ActionBar标题设置为中间位置而不是默认的左对齐位置?
尝试将ActionBar标题设置为中间位置,而不是默认的左对齐位置。为此,根据其他答案,我做了以下工作:Java 如何将ActionBar标题设置为中间位置而不是默认的左对齐位置?,java,android,xml,android-actionbar,Java,Android,Xml,Android Actionbar,尝试将ActionBar标题设置为中间位置,而不是默认的左对齐位置。为此,根据其他答案,我做了以下工作: Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbarDownloads); setSupportActionBar(myToolbar); if(getSupportActionBar()!=null) { getSupportActionBar().setDisplayOptions(
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbarDownloads);
setSupportActionBar(myToolbar);
if(getSupportActionBar()!=null)
{
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar_layout);
}
actionbar_layout.xml
然而,这并不能产生预期的结果,也就是说,这使得文本仍然是左对齐的,这种方法有什么问题,如何解决这个问题 尝试使用自定义工具栏
尝试使用自定义工具栏
您需要在xml文件中使用工具栏,并且可以在工具栏中使用TextView。您可以将自定义视图添加到工具栏中。完成xml文件后,就可以将工具栏定义到活动类中
您需要在xml文件中使用工具栏,并且可以在工具栏中使用TextView。您可以将自定义视图添加到工具栏中。完成xml文件后,就可以将工具栏定义到活动类中
简单的方法是使用android:layout_gravity=在文本视图上居中,然后使用下面的代码隐藏默认标题,然后初始化文本视图并设置值
在您的活动中:
自定义工具栏XML代码:
简单的方法是使用android:layout_gravity=在文本视图上居中,然后使用下面的代码隐藏默认标题,然后初始化文本视图并设置值
在您的活动中:
自定义工具栏XML代码:
你不需要一个工具栏。只需在活动中使用类似于theme.AppCompat.Light.DarkActionBar的主题和ActionBar,并使用以下代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar_layout);
}
//your code
}
你不需要一个工具栏。只需在活动中使用类似于theme.AppCompat.Light.DarkActionBar的主题和ActionBar,并使用以下代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar_layout);
}
//your code
}
是否尝试添加getSupportActionBar.setDisplayShowCustomEnabled;在getSupportActionBar.setCustomViewR.layout.actionbar_layout;之前?是否尝试添加getSupportActionBar.setDisplayShowCustomEnabled;在getSupportActionBar.setCustomViewR.layout.actionbar_layout;之前@Nilesh,感谢您的快速方法,但我不能在自定义布局文件中使用工具栏,还有其他元素取决于活动中的工具栏,即我确实有一个自定义SmartTableLayout与工具栏一起运行,因此不能在自定义布局文件中为actionbar使用工具栏:/Ok,我知道了,lemme try:@OBX如果有任何疑问,请告诉我。如需魅力:再次感谢@Nilesh,感谢您的快速方法,但我不能在自定义布局文件中使用工具栏,还有其他元素取决于活动中的工具栏,即我确实有一个自定义SmartTableLayout与工具栏一起运行,因此不能在自定义布局文件中为actionbar使用工具栏:/Ok,我知道了,lemme try:@OBX如果有任何疑问,请告诉我。如需魅力:再次感谢!
private Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
mTitle.setText("Nilesh Rathod");
setSupportActionBar(toolbar);
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="58dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:titleTextColor="#ffffff">
<TextView
android:id="@+id/mytext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Downloads"
android:textColor="#000000"
android:textSize="18sp" />
</android.support.v7.widget.Toolbar>
//Rest of your code
</LinearLayout>
Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
toolbar.setTitle("");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar_title = (TextView) findViewById(R.id.toolbar_title);
toolbar_title.setText("My Custom center title");
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Toolbar Title" />
</android.support.v7.widget.Toolbar>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar_layout);
}
//your code
}
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:minHeight="?attr/actionBarSize"
app:contentInsetStart="0dp">
<TextView
android:id="@+id/back_txt"
style="@android:style/TextAppearance.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="Back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Downloads"
android:layout_gravity="center|center_horizontal|center_vertical"
android:layout_marginTop="20dp"
android:layout_centerInParent="true"
android:textColor="#000000"
android:id="@+id/mytext"
android:textSize="18sp" />
<TextView
android:id="@+id/filter_btn"
style="@style/Base.TextAppearance.AppCompat.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical|center_horizontal"
android:layout_margin="10dp"
android:drawablePadding="5dp"
android:gravity="center"
android:text="Back"
android:textColor="@android:color/black" />
</android.support.v7.widget.Toolbar>
</LinearLayout>