2013-07-26 73 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>