Android Action Bar 自訂View

來源:互聯網
上載者:User

 

 


actionbar自訂view的布局檔案action_bar.xml

 

[html] 
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:gravity="center_vertical" 
    android:orientation="horizontal"> 
    <TextView 
        android:id="@+id/action_bar_title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textColor="#EEA9B8" 
        android:text="@string/action_bar_title" /> 
    <Spinner 
        android:id="@+id/action_bar_spinner" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" > 
    </Spinner> 
</LinearLayout> 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/action_bar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#EEA9B8"
        android:text="@string/action_bar_title" />
    <Spinner
        android:id="@+id/action_bar_spinner"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >
    </Spinner>
</LinearLayout>Spinner Item的布局, 借用了之前抽屜導航Item的布局drawer_item.xml

 

[html] 
<?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="match_parent" 
    android:gravity="center_vertical" 
    android:orientation="horizontal" > 
    <ImageView 
        android:id="@+id/drawer_img" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/> 
    <TextView 
        android:id="@+id/drawer_title" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"/> 
</LinearLayout> 

<?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="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal" >
    <ImageView
        android:id="@+id/drawer_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/drawer_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
布局準備好了, 在Activity裡面寫一些.
首先是onCreate()方法裡面準備一下.
[java] 
/** action_bar添加自訂view */ 
View actionbarLayout = LayoutInflater.from(this).inflate(R.layout.action_bar, null); 
mActionbarSpinne = (Spinner) actionbarLayout.findViewById(R.id.action_bar_spinner); 
//setAdapter可以根據具體情況, 此處不必深究.  
mActionbarSpinne.setAdapter(new SimpleAdapter(this, 
        getDrawerItems(true), R.layout.drawer_item, new String[] { 
                "drawer_img", "drawer_title" }, new int[] { 
                R.id.drawer_img, R.id.drawer_title })); 
mActionbarSpinne.setOnItemSelectedListener(new SpinnerItemSelectedListener()); 
//使自訂的普通View能在title欄顯示, actionBar.setCustomView能起作用.  
actionBar.setDisplayShowCustomEnabled(true); 
actionBar.setCustomView(actionbarLayout); 

/** action_bar添加自訂view */
View actionbarLayout = LayoutInflater.from(this).inflate(R.layout.action_bar, null);
mActionbarSpinne = (Spinner) actionbarLayout.findViewById(R.id.action_bar_spinner);
//setAdapter可以根據具體情況, 此處不必深究.
mActionbarSpinne.setAdapter(new SimpleAdapter(this,
        getDrawerItems(true), R.layout.drawer_item, new String[] {
                "drawer_img", "drawer_title" }, new int[] {
                R.id.drawer_img, R.id.drawer_title }));
mActionbarSpinne.setOnItemSelectedListener(new SpinnerItemSelectedListener());
//使自訂的普通View能在title欄顯示, actionBar.setCustomView能起作用.
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionbarLayout);然後是

[java]
/**
 * 監聽action_bar的spinner item點擊事件
 */ 
private class SpinnerItemSelectedListener implements OnItemSelectedListener { 
    @Override 
    public void onItemSelected(AdapterView<?> arg0, View view, int position, 
            long arg3) { 
        selectItem(view, position, MESSAGE_SPINNER_ITEM); 
    } 
    @Override 
    public void onNothingSelected(AdapterView<?> arg0) {} 

/**
 * 監聽action_bar的spinner item點擊事件
 */
private class SpinnerItemSelectedListener implements OnItemSelectedListener {
    @Override
    public void onItemSelected(AdapterView<?> arg0, View view, int position,
            long arg3) {
        selectItem(view, position, MESSAGE_SPINNER_ITEM);
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {}
}Listener的selectItem()細節如下


[java] 
if (messageId == MESSAGE_SPINNER_ITEM) { 
            if (position != 0) { 
                TextView title = (TextView) view.findViewById(R.id.drawer_title); 
                String cityName = title.getText().toString(); 
                this.cityInput.setText(cityName); 
            } 
        } 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.