Android 사용자 정의 ActionBar 인 스 턴 스

이 사례 는 안 드 로 이 드 가 Action Bar 를 사용자 정의 하 는 실현 방법 을 설명 한다.모두 에 게 참고 하도록 공유 하 다.구체 적 인 실현 방법 은 다음 과 같다.
Android 3.0 이상 에는 ActionBar 의 API 가 있 습 니 다.support package 를 도입 하여 3.0 이하 의 플랫폼 에서 이 API 를 참조 할 수 있 습 니 다.그러나 여 기 는 ActionBar 를 완전히 사용자 정의 합 니 다.추가 jar 패 키 지 를 도입 하지 않 아 도 됩 니 다.원본 UI 구성 요소 인 GreeenDroid 를 참조 하 십시오.프로젝트 홈 페이지:https://github.com/cyrilmottier/GreenDroid 。Action Bar 에 관 한 파일 을 추출 하면 자신의 항목 에 넣 고 마지막 으로 다운로드 주 소 를 첨부 할 수 있 습 니 다.다음은 프로그램 에서 의 용법 을 말씀 드 리 겠 습 니 다.
TestAction Bar 프로젝트 를 새로 만 듭 니 다.관련 코드 를 com.leaf.actionbar 라 는 가방 에 넣 었 다 고 가정 하 십시오.먼저 레이아웃 파일 을 새로 만 듭 니 다.main.xml,다음 과 같 습 니 다.
  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:bar="http://schemas.android.com/apk/res/com.leaf.actionbar"  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
     
    <com.leaf.actionbar.ActionBar 
        android:id="@id/gd_action_bar" 
        android:layout_height="@dimen/gd_action_bar_height" 
        android:layout_width="fill_parent" 
        android:background="?attr/gdActionBarBackground" 
        bar:type="normal" 
        bar:title="Test ActionBar" /> 
</LinearLayout>
이 사용자 정의 Action Bar 는 사실 Linearlayout 이 고 자신의 속성 도 있 습 니 다.먼저 네 임 스페이스 를 정의 해 야 합 니 다.이름 은 위 에 있 는 bar 와 같 습 니 다.title 은 actionbar 의 제목 입 니 다.type 은 이 actionbar 의 왼쪽 아 이 템 을 구분 하 는 레이아웃 입 니 다.세 가지 유형 이 있 습 니 다.하 나 는 normal 입 니 다.왼쪽 은 홈 페이지 를 표시 하 는 imagebutton 과 title 을 표시 하 는 textview 입 니 다.하 나 는 dashboard 이 고 왼쪽 은 app 아이콘 을 표시 하 는 imageview 와 title 을 표시 하 는 textview 입 니 다.하 나 는 empty 이 고 왼쪽 에는 title 을 표시 하 는 textview 만 있 습 니 다.오른쪽 은 모두 자신 이 추가 한 단추 입 니 다.기본 값 은 normal 입 니 다.다음:normal,dashboard,empty 에 대응 합 니 다.

그리고 일부 속성 이 있 습 니 다.homeDrawable 은 왼쪽 그림 을 바 꿀 수 있 습 니 다.기본 값 은 홈 페이지 의 그림 입 니 다.dividerDrawable:분할 선,dividerWidth:분할 선 너비,maxItems:오른쪽 에 추 가 된 item 의 최대 갯 수 입 니 다.추가 선택 가능.
이어서 MainActivity.java.
public class MainActivity extends Activity {  
 
    private ActionBar mActionBar; 
    private final Handler mHandler = new Handler(); 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        // type normal  
        mActionBar = (ActionBar) findViewById(R.id.gd_action_bar); 
        // item, imagebutton 
        // type, refresh、search, imagebutton src 
        // imagebutton id, values/ids.xml  
        // type , imagebutton, progressbar,  
        mActionBar.addItem(Type.Refresh, R.id.action_bar_refresh); 
        mActionBar.addItem(Type.Search, R.id.action_bar_search); 
        // item,  
        mActionBar.addItem( 
                mActionBar.newActionBarItem(NormalActionBarItem.class) 
                        .setDrawable(R.drawable.gd_action_bar_eye) 
                        .setContentDescription("view"), R.id.action_bar_view); 
        // item imagebutton  
        mActionBar.setOnActionBarListener(new OnActionBarListener() { 
 
            @Override 
            public void onActionBarItemClicked(int position) { 
 
                if (position == ActionBar.OnActionBarListener.HOME_ITEM) { 
 
                    //  
                    Toast.makeText(MainActivity.this, "home or back", 
                            Toast.LENGTH_SHORT).show(); 
                    return; 
 
                } 
 
                final ActionBarItem item = mActionBar.getItem(position); 
 
                switch (item.getItemId()) { 
                case R.id.action_bar_refresh: 
                    if (item instanceof LoaderActionBarItem) { 
 
                        mHandler.postDelayed(new Runnable() { 
                            @Override 
                            public void run() { 
                                // progressbar 
                                ((LoaderActionBarItem) item).setLoading(false); 
                            } 
                        }, 2000); 
                    } 
 
                    Toast.makeText(MainActivity.this, "refresh", 
                            Toast.LENGTH_SHORT).show(); 
                    break; 
 
                case R.id.action_bar_search: 
 
                    //  
                    Toast.makeText(MainActivity.this, "search", 
                            Toast.LENGTH_SHORT).show(); 
                    break; 
 
                case R.id.action_bar_view: 
 
                    //  
                    Toast.makeText(MainActivity.this, "view", 
                            Toast.LENGTH_SHORT).show(); 
                    break; 
                } 
            } 
        }); 
    } 
}
마지막 으로 주의해 야 할 것 은 AndroidManifest.xml 파일 에서:

        android:icon="@drawable/ic_launcher" 
        android:label="testactionbar"
        android:theme="@style/Theme.GreenDroid">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" /> 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</application>
이 문장 을 추가 하지 않 으 면 잘못 보 고 됩 니 다.이 테 마 는 parent="android:Theme"에서 계승 합 니 다.그 안에 사용자 정의 style 이 있 습 니 다.window 의 title 표시 줄 을 지 우려 면 이 theme 파일 에'item name='android:windowNoTitle'>true을 추가 해 야 합 니 다.
마지막 효과 그림:

프로젝트 인 스 턴 스 코드 는 여 기 를 클릭 하 십시오본 사이트 다운로드
이 글 이 여러분 의 안 드 로 이 드 프로 그래 밍 에 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기