2013-07-26 71 views
0

我需要一點幫助來自定義複選框的外觀。 我的XML文件是如何更改Android複選框樣式

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" > 
    <TextView 
     android:id="@+id/titleTextView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:padding="10dp" 
     android:paddingLeft="5dp" 
     android:paddingRight="5dp" 
     android:text="@string/preferances_title" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textSize="26sp" /> 
    <CheckBox 
     android:id="@+id/soundCheckBox" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:gravity="center_vertical" 
     android:paddingBottom="10dp" 
     android:text="@string/soundEnabled" 
     android:textSize="20sp" /> 
</LinearLayout> 

它看起來像這樣

enter image description here

我希望我的複選框看起來像這樣(有標題和描述)

enter image description here

我怎麼能去做?

回答

1

那麼因爲(從我的理解)你想創建一個首選項屏幕。

我建議你使用PreferenceFragment或PreferenceActivity,它從xml中擴展它的佈局 。

目前在互聯網上的許多例子只是搜索:偏好屏幕教程

我相信第二圖像使用CheckBoxPreference, 所以,如果你想創造這樣你可以在使用第二圖像您的xml 將具有完全相同的視圖。

<CheckBoxPreference 
      android:defaultValue="false" 
      android:icon="@drawable/image" 
      android:key="preference_key" 
      android:summary="@string/preference_summary" 
      android:title="@string/preference_title" /> 

注意CheckBoxPreference不能在所有的佈局,但只能在PreferenceActivity

不然,如果你想創建這樣的事情,但沒有一個首選項,你可以使用 CheckedTextView具有使用一個複選框的右側,但沒有字幕,你將需要添加爲您的佈局文本視圖。

+1

在Android上設置屏幕的官方文檔是在這裏:http://developer.android.com/guide/topics/ UI/settings.html –

0

您應該使用RelativeLayout第二圖像中獲得的佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="20dp" 
     android:layout_marginLeft="20dp" 
     android:text="Lock screen notifications" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_below="@+id/textView1" 
     android:text="Show notifications on lock screen" /> 

    <CheckBox 
     android:id="@+id/checkBox1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/textView2" 
     android:layout_alignParentRight="true" /> 

</RelativeLayout>