0

我需要一些帮助来自定义复选框的外观。我的 xml 文件是

<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>

看起来像这样

在此处输入图像描述

我希望我的复选框看起来像这样(带有标题和描述)

在此处输入图像描述

我怎样才能做到这一点?

4

2 回答 2

1

好吧,因为(据我了解)您想创建一个偏好屏幕。

我建议您使用 PreferenceFragment 或 PreferenceActivity 从 xml 扩展其布局。

网上有很多例子,随便搜一下:preference screen tutorial

我相信第二张图片使用 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,它右侧有一个复选框,但没有您需要在布局中添加为文本视图的字幕。

于 2013-07-26T21:27:49.337 回答
0

您应该使用 aRelativeLayout来获取第二张图片中的布局:

<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>
于 2013-07-26T21:48:38.340 回答