▶ Custom Title

실행화면



소스 위치 : src/com/example/android/apis/app/CustomTitle.java

원래는 타이틀에 한줄로 "App/Activity/Custom Dialog"라는 글자만 출력되야 하나
Title의 layout을 변경함으로써 "Left is best             Right is always right" 형태로 두개의 TextView를 사용할수 있게 된다.
일반적인 Title의 경우 흰색이었는데 Custom Title의 경우 Text의 색상이 디폴트로 회색으로 설정되는것 같다. 
(변경하려면 layout xml에서 직접 TextView의 속성에 android:textColor를 설정하면 된다.)

Title Bar를 원하는 데로 수정하기 위해서 requestWindowFeature() 함수 사용
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

public final boolean requestWindowFeature(int featureId);
: 윈도우의 기능확장을 활성화 한다.
Enable extended window features. This is a convenience for calling getWindow().requestFeature()


사용되는 feature ID는 다음과 같다.
Constants
int DEFAULT_FEATURES The default features enabled
int FEATURE_CONTEXT_MENU Flag for the context menu.
int FEATURE_CUSTOM_TITLE Flag for custom title.
int FEATURE_INDETERMINATE_PROGRESS Flag for indeterminate progress
int FEATURE_LEFT_ICON Flag for having an icon on the left side of the title bar
int FEATURE_NO_TITLE Flag for the "no title" feature, turning off the title at the top of the screen.
int FEATURE_OPTIONS_PANEL Flag for the "options panel" feature.
int FEATURE_PROGRESS Flag for the progress indicator feature
int FEATURE_RIGHT_ICON Flag for having an icon on the right side of the title bar
int ID_ANDROID_CONTENT The ID that the main layout in the XML layout file should have.
int PROGRESS_END Ending value for the (primary) progress
int PROGRESS_INDETERMINATE_OFF Flag for setting the progress bar's indeterminate mode off
int PROGRESS_INDETERMINATE_ON Flag for setting the progress bar's indeterminate mode on
int PROGRESS_SECONDARY_END Highest possible value for the secondary progress
int PROGRESS_SECONDARY_START Lowest possible value for the secondary progress
int PROGRESS_START Starting value for the (primary) progress
int PROGRESS_VISIBILITY_OFF Flag for setting the progress bar's visibility to GONE
int PROGRESS_VISIBILITY_ON Flag for setting the progress bar's visibility to VISIBLE
CustomTitle Class와 매치되는 XML은 custom_title.xml

        setContentView(R.layout.custom_title);

custom_title.xml 파일 내용
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/screen"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false">
        <EditText android:id="@+id/left_text_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxEms="10"
            android:minEms="10"
            android:layout_gravity="center_vertical"
            android:text="@string/custom_title_left" />
        <Button android:id="@+id/left_text_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/custom_title_left_button"/>
    </LinearLayout>
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false">
        <EditText android:id="@+id/right_text_edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxEms="10"
            android:minEms="10"
            android:layout_gravity="center_vertical"
            android:text="@string/custom_title_right" />
        <Button android:id="@+id/right_text_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="@string/custom_title_right_button"/>
    </LinearLayout>
</LinearLayout>


Left와 Right 버튼 Listener 설정
버튼을 누르면 Title Bar의 왼쪽과 오른쪽의 TextView의 내용이 변경된다.

        leftButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                leftText.setText(leftTextEdit.getText());
            }
        });
        rightButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                rightText.setText(rightTextEdit.getText());
            }
        });

custom Title 부분을 cutom_tilte_i 로 설정하기 위해 setFeatureInt 함수를 사용한다.
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_1);

public abstract void setFeatureInt(int featureId, int value)
: Feature에 대한 값을 Integer값으로 설정한다. 각 Feature에 해당하는 값의 범위로 설정
Set the integer value for a feature. The range of the value depends on the feature being set. For FEATURE_PROGRESSS, it should go from 0 to 10000. At 10000 the progress is complete and the indicator hidden


custom_title_1.xml 파일 내용
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/screen"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView android:id="@+id/left_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/custom_title_left" />
    <TextView android:id="@+id/right_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="@string/custom_title_right" />
</RelativeLayout>


신고

'Android > ApiDemos' 카테고리의 다른 글

App - Activity - Forwarding  (0) 2010.03.17
App - Activity - Dialog  (0) 2010.03.17
App - Activity - Custom Title  (0) 2010.03.17
App - Activity - Custom Dialog  (0) 2010.03.17
App - Activity - Animation  (0) 2010.03.17
App Demos - App (1/10) Activity  (0) 2010.03.16
Posted by 까칠코더.


티스토리 툴바