在自定义ActionBar中水平居中TextView

问题描述:

想要创建自定义ActionBar,并在其中央应用程序名称,但它仍然对齐到左侧。这里是custom_actionbar.xml在自定义ActionBar中水平居中TextView

<?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> 

这里的XML是创建活动:

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); 
    } 
... 
} 

你能不能指点我究竟做错了什么? 感谢

+0

尝试用LinearLayout中,并删除这一行android:layout_centerInParent =“true” – cuoka

更改TextView小号宽度match_parent和应用android:gravity="center_horizontal"

采用Android支持ToolBar自定义动作条..

使用这个链接Tool bar参考