创建于preferences自定义布局 [英] Creating a custom layout for preferences

查看:155
本文介绍了创建于preferences自定义布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建用于管理preferences自定义布局。我知道有标准和preferenceActivity提供建议的布局,但如果我想补充,也就是说,一个按钮,我怎么去这样做?

I'm trying to create a custom layout for managing preferences. I know there is the standard and recommended layout provided by PreferenceActivity, but if I want to add, say, a Button, how do I go about doing that?

我计划设计的应用程序,以便当用户触摸的主要活动屏幕上的某个项目,它关系到一个选项子菜单,用户可以启用,随后出现的下一个屏幕上(它有在这个线性进程)。用户必须将这些他使用的应用程序每次设定。目前,我正在考虑使用该标准的preferenceActivity重新present这个子菜单的选项。

I plan to design the application so that when the user touches a certain item on the main activity screen, it goes to a 'submenu' of options that the user can enable, which subsequently appears on the next screen (it has to be in this linear progression). The user must set these each time he uses the application. Currently, I'm considering using the standard PreferenceActivity to represent this submenu of options.

也许这是不正确的方法呢?

Maybe this isn't the right approach?

编辑:我忽略了一个解决方案,而谷歌搜索。 如何添加一个按钮,preferenceScreen

I overlooked the solution while Googling. How to add a button to PreferenceScreen

推荐答案

您可以随时创建一个自定义preference版面项目,并用它在preferenceActivity。例如,我这样做:

You can always create a custom preference layout item and use it in the PreferenceActivity. For example, I did this:

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

它有一些浪费,但基本上建立2行(标题,副标题)在右边的图像。你可以用一个按钮代替ImageView的,你会被所有的设置。然后,设置布局在各个preference你的 XML / prefs.xml 文件,就像这样:

<Preference
    android:title="@string/label_pref_version"
    android:key="@string/pref_version"
    android:layout="@layout/pref" />

在你的活动code,只是火 findViewById 并附加一个监听器的按钮。可能有,如果您在活动多个按钮做一些更多的工作,但不应该是不合理的。

After that just fire findViewById in your Activity code and attach a listener to the button. Might have to do some more work if you have multiple buttons in the Activity, but shouldn't be unreasonable.

这篇关于创建于preferences自定义布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆