2016-04-30 42 views
0

我得到了一個帶有3個菜單項目的操作欄,可悲的是這些菜單項在左側對齊,我會在中心對齊。我一直在環顧四周,嘗試了一些東西,但沒有任何工作。如何在Android上的動作欄中對齊中心項目

這裏是我到目前爲止的代碼:

public class Layout extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.actionbar); 
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    getSupportActionBar().setCustomView(R.layout.actionbar); 

} 

public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.mainmenu, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     // action with ID action_refresh was selected 
     case R.id.action_refresh: 
      Toast.makeText(this, "Refresh selected", Toast.LENGTH_SHORT) 
        .show(); 
      break; 
     // action with ID action_settings was selected 
     case R.id.action_settings: 
      Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT) 
        .show(); 
      break; 
     default: 
      break; 
    } 

    return true; 
} 

} 

,這裏是我的R.layout.action:

<?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" 
    android:gravity="center" 
    /> 

回答

0

的事情是,你是不是真的應該居中對齊在ActionBar(或工具欄,取決於你正在使用的)中的文本 - 這就是爲什麼它不是簡單的事情。

如果您將其保持爲當前對齊狀態,則應該與72dp角線對齊(它將與通用材質指南相匹配)。

您將在這裏找到這些規範:https://www.google.com/design/spec/layout/structure.html#structure-app-bar(但同樣,如果你什麼都不做,一切都應該正常工作)

+0

感謝您的評論。可悲的是,這並沒有真正幫助解決我的問題,這可能不是正確的做法,但這是我在Android應用程序中看到的很多東西。也許actionBar不是我的情況下使用的組件,如果是的話,哪一個會是? –

+0

那麼使用ActionBar以外的東西將非常困難(有許多功能內置到ActionBar中,您將不得不復制)。也只是因爲你看到另一個應用程序這樣做,並不意味着它很容易(或者說它應該完成) – Booger

0

看,你可能已經看到了一些有動作欄(工具欄)上居中的文本的應用程序,但是必須定製佈局或者使用基本編碼來實現這種情況,API應該大於5.0。