본문 바로가기

나의 플랫폼/안드로이드

[ Android ] Custom Preference.

336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

Preference를 Custom하게 만들고 싶을 경우 다음과 같이 해보아라.


먼저, Custom 하게 만들기 위해서 기본 Prefernce의 layout형태를 파악 해야합니다.

http://stackoverflow.com/questions/6194116/creating-a-custom-layout-for-preferences

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignLeft="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:maxLines="2" />

        <ImageView android:id="@+id/ImageView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/go"
            android:layout_alignParentRight="true" />

    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@+android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:orientation="vertical" />

</LinearLayout>

위 소스 중에 주석처리가 되어 있는 부분을 해석해 보시면.

actual preference는 여기애 놓아라!! 라는 말이 있죠!

저 LinearLayout안에 원하는 Custom Layout을 넣으시면 preference가 Custom 하게 바뀝니다.


중요한거!!!

원래 Preference 속성에서 android:widgetLayout 에 custom한 layout xml만 지정을 해줘도 

문제 없이 Custom하게 바뀝니다.


하지만!!!

RTL 언어를 대응할 경우, 분명 match_parent를 주었는데 한 쪽이 비어버리는 현상이 발생 합니다.

따라서 위와 같이 기본 Layout 형태에서 Custom하게 바꿔줘야지만

RTL 언어를 설정하더라도 문제 없이 layout이 설정이 됩니다.


Preference 높이 조절 하기

Preference를 상속 받은 Class에서 아래 getView 함수를 overriding 하여 넣어 주시면, 80dip로 preference 높이를 강제적으로 수정이 가능합니다.

// [cgyu.yu][TD:95198] Problem from string size

    @Override

    public View getView(View convertView, ViewGroup parent) {

        final View v = super.getView(convertView, parent);

        final int width = android.view.ViewGroup.LayoutParams.MATCH_PARENT;

        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 80, mContext.getResources().getDisplayMetrics());

        final int height = (int)px;

        final LayoutParams params = new LayoutParams(width, height);

        v.setLayoutParams(params);

        return v;

    }


'나의 플랫폼 > 안드로이드' 카테고리의 다른 글

[Android Studio] error occurred during initialization of vm  (6) 2015.06.03
git 관련 명령어  (0) 2013.10.24
정규식 표현  (0) 2013.09.29
객체 File 저장 클래스  (0) 2013.09.28
[ Android ] Custom NumberPicker  (0) 2013.09.25