我是android開發新手。 我正在研究一個小項目,應用程序的標題應該位於操作欄左側的中心和文本視圖中。使用Action Bar Android開發
我想知道這種方法是否正確。 1.我已經隱藏使用
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
2.I開發佈局,以取代這樣
的動作條因此,這是正確的是這樣做的動作條? 如果不是,那麼正確的方法是什麼?
我是android開發新手。 我正在研究一個小項目,應用程序的標題應該位於操作欄左側的中心和文本視圖中。使用Action Bar Android開發
我想知道這種方法是否正確。 1.我已經隱藏使用
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
2.I開發佈局,以取代這樣
的動作條因此,這是正確的是這樣做的動作條? 如果不是,那麼正確的方法是什麼?
你的方法是正確的,但請進行Slidenerd材料設計教程。您可以與您specification.Following網站幫助創建自己的工具欄你: Slidenerd toolbar tutorial
是的,完美的。
的微小變化可以代替隱藏現有的操作欄你可以用你的自定義視圖替換它(現在讓我們假設它是custom_actionbar.xml)。您的代碼是這樣的:
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
添加下面的代碼。它會工作。
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowCustomEnabled(true);
LayoutInflater inflater = LayoutInflater.from(this);
View v = inflater.inflate(R.layout.action_bar_title, null);
TextView titleTextView = (TextView) v.findViewById(R.id.custom_action_bar_title);
titleTextView.setText("Title");
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(icon);
actionBar.setCustomView(v);
按照這種方式
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
mTitleTextView.setText("My Own Title");
ImageButton imageButton = (ImageButton) mCustomView
.findViewById(R.id.imageButton);
imageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Refresh Clicked!",
Toast.LENGTH_LONG).show();
}
});
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
和custom_actionbar.xml
<?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="50dp"
android:background="@drawable/black_pattern" >
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAllCaps="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff"
android:textStyle="bold" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="8dp"
android:src="@drawable/ic_launcher" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:background="@null"
android:src="@android:drawable/ic_menu_rotate" />
</RelativeLayout>
希望這將有助於你
我會建議你使用的系統爲Android Lollipop版本,但與getSupportActionbar(工具欄提供的API),使用這 -
actionBar.setCustomView(mCustomView);
actionBar.setDisplayShowCustomEnabled(true);
自定義工具欄添加到您的佈局
<android.support.v7.widget.Toolbar
android:id="@+id/custom_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"
>
<TextView
android:id="@+id/tv_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="@color/white"
android:text="Edit"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="5dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="@color/white"
android:text="Movies"
/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
這個代碼添加到您的Java類,您的onCreate()內
Toolbar custom_toolbar= (Toolbar) findViewById(R.id.custom_toolbar);
setSupportActionBar(custom_toolbar);
希望這適用於您,祝您好運....