使普通布局看起来像PreferenceScreen [英] Make a normal layout look like a PreferenceScreen

查看:122
本文介绍了使普通布局看起来像PreferenceScreen的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PreferenceScreen对我来说还不够好,因为我必须将项目添加到微调器中.这些项目需要来自数据列表.

The PreferenceScreen isn't good enough for me, since I've to add items to a Spinner. Those items need to come from a data list.

我有一个自定义的ArrayAdapter,它返回项的名称以及单击时的名称.它返回包含在项目中的数据.

I've got a custom ArrayAdapter that returns the name of the item, and when I click it. It returns the data that is contained within the item.

我想在ListPreference(这是PreferenceScreen中的微调器)中使用相同的ArrayAdapter,但是ListPreference不允许我使用适配器.

I want to use that same ArrayAdapter in a ListPreference (that's the spinner in the PreferenceScreen) but the ListPreference doesn't allow me to use a Adapter.

因此,我想不使用实际的PreferenceScreen(和PreferenceCategory)来重新创建PreferenceScreen(具有PreferenceCategory)的外观

So, I want to recreate the look of the PreferenceScreen (with the PreferenceCategory's) without the use of the actual PreferenceScreen (and PreferenceCategory's)

图书馆有可能吗?我还没有找到.

Is this possible with a library? I haven't found one.

谢谢

蒂姆

推荐答案

上面的答案难以实现,因此我设计了自己的版本.

The answer above is to difficult to implement, so I've designed my own version.

布局xml

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

<include layout="@layout/toolbar"/> <!-- This is a custom toolbar (or actionbar), and not necessary -->

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/toolbar"
        android:paddingRight="?android:attr/scrollbarSize">

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@layout/toolbar"
            android:id="@+id/scrollView" >
            <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:text="@string/category_battery"
                    android:id="@+id/category_misc"
                    android:layout_marginLeft="@dimen/activity_settings_header_margin" />

                <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="2dp"
                    android:id="@+id/divider"
                    android:layout_marginLeft="@dimen/activity_settings_margin"
                    android:layout_below="@+id/category_misc"
                    android:contentDescription="divider"
                    android:scaleType="matrix"
                    android:background="@android:drawable/divider_horizontal_bright"
                    android:src="@android:drawable/divider_horizontal_bright" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:padding="@dimen/activity_settings_margin">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/textView"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/textView"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/textView"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/textView"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/textView"/>

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/textView"/>

                </LinearLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/category_calibration"
                    android:textAppearance="?android:attr/textAppearanceSmall"
                    android:id="@+id/category_subjects"
                    android:layout_marginLeft="@dimen/activity_settings_header_margin"
                    android:layout_below="@+id/batteryChargeState" />

                <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="2dp"
                    android:id="@+id/divider2"
                    android:layout_marginLeft="@dimen/activity_settings_margin"
                    android:layout_below="@+id/category_subjects"
                    android:contentDescription="divider"
                    android:scaleType="matrix"
                    android:background="@android:drawable/divider_horizontal_bright"
                    android:src="@android:drawable/divider_horizontal_bright" />



                <LinearLayout android:layout_width="match_parent"
                    android:layout_below="@+id/category_subjects"
                    android:layout_centerVertical="true"
                    android:layout_height="match_parent"
                    android:padding="@dimen/activity_settings_margin"
                    android:orientation="vertical"
                    android:id="@+id/nextLayout">


                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/textView"/>


                </LinearLayout>

            </LinearLayout>

        </ScrollView>
    </RelativeLayout>


</RelativeLayout>

工具栏xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"/>

Dimens xml:

Dimens xml:

<resources>
    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

    <dimen name="activity_settings_margin">24dp</dimen>
    <dimen name="activity_settings_header_margin">18dp</dimen>
</resources>

颜色xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="orange">#FDA432</color>
    <color name="orange_dark">#ffd17731</color>

</resources>

只需使用您的方式存储首选项.我已经创建了一个包含私钥的自定义首选项类,因此我无法在不破坏代码的情况下在此处发布代码.

Just use the your way to store the Preferences. I've created a custom preference class that contains private keys so I can't post the code here without breaking it.

使用这样的自定义布局的优点是,您可以使用此行添加自己的工具栏作为第一个RelativeLayout的第一个元素.

The advantage of using a custom layout like this is that you can add your own toolbar with this line as the first element of the first RelativeLayout.

要使用自定义工具栏,请在onCreate()中使用以下代码

To use the custom toolbar use this piece of code in your onCreate()

    @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                mTitle = mDrawerTitle = getTitle(); // This is for the title when you use a drawer

                mToolbar = (Toolbar) findViewById(R.id.toolbar); // This finds the toolbar you've specified using the <include> in the xml

                setSupportActionBar(mToolbar); // This sets the toolbar to be used

                mToolbar.setBackgroundColor(getResources().getColor(R.color.orange)); // This sets the color of the toolbar

                if (Build.VERSION.SDK_INT >= 21) {
                    getWindow().setStatusBarColor(getResources().getColor(R.color.orange_dark)); // This sets the color of the navigation bar to a darker orange as used for the toolbar, only when this is supported!
                }

                mToolbar.setNavigationIcon(R.mipmap.ic_launcher); // This makes the icon clickable, to open and close a drawer if you have one
    }

这篇关于使普通布局看起来像PreferenceScreen的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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