Android 将自定义操作栏中的文本视图水平居中
A想要创建中间有应用程序名称的自定义ActionBar,但它仍然向左对齐。下面是custom_actionbar.xml的xml 你能告诉我我做错了什么吗?Android 将自定义操作栏中的文本视图水平居中,android,android-layout,textview,Android,Android Layout,Textview,A想要创建中间有应用程序名称的自定义ActionBar,但它仍然向左对齐。下面是custom_actionbar.xml的xml 你能告诉我我做错了什么吗? 谢谢将TextViews宽度更改为match\u parent并应用android:gravity=“center\u horizontal”将TextViews宽度更改为match\u parent并应用android:gravity=“center\u horizontal”使用android支持工具栏定制actionbar 使用此链接
谢谢将
TextView
s宽度更改为match\u parent
并应用android:gravity=“center\u horizontal”将TextView
s宽度更改为match\u parent
并应用android:gravity=“center\u horizontal”
使用android支持工具栏
定制actionbar
使用此链接作为参考使用android支持工具栏
作为自定义操作栏
使用此链接作为参考尝试使用线性布局,并删除此行android:layout\u centerInParent=“true”尝试使用线性布局,并删除此行android:layout\u centerInParent=“true”
<?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="wrap_content"
android:gravity="center"
android:background="@color/transparent">
<TextView
android:id="@+id/nav_bar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@color/transparent"
android:singleLine="true"
android:textColor="@color/sue_green"
android:text="@string/app_name"
android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
</RelativeLayout>
public class DeviceOverviewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR);
setTheme(R.style.Theme_SUE);
setContentView(R.layout.activity_device_overview);
final ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayUseLogoEnabled(false);
LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.custom_actionbar, null);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(v);
}
...
}