9

我有一个首选项屏幕(responder_generic.xml),如下所示:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <Preference
        android:defaultValue="false"
        android:key="auto_responder_key"
        android:layout="@layout/preference_header_switch_item_responder"
        android:title="@string/auto_responder">
    </Preference>

</PreferenceScreen>

我将其实例化如下:(在 2.3.3 版中)

addPreferencesFromResource(R.xml.responder_generic);

我的布局 =preference_header_switch_item_responder如下所示:

<?xml version="1.0" encoding="utf-8"?>
<!-- Layout of a header item in PreferenceActivity. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="33dp"
    android:gravity="center_vertical"
    android:minHeight="33dp"
    android:id="@+id/preference_header_switch_item_responder"
    style="?android:attr/listSeparatorTextViewStyle" >

    <RelativeLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_weight="1" >

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal"
            android:singleLine="true"
            android:text="@string/auto_responder"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:ellipsize="end"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </RelativeLayout>

    <include layout="@layout/compound_button" />

</LinearLayout>

现在,我在layout文件夹中定义了复合按钮,layout/v-14分别如下:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" >

<CheckBox
    android:id="@+id/switchWidget"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:clickable="true"
    android:focusable="false"
    android:padding="8dip"
    android:layout_marginRight="14dip" />

</merge>

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Switch
        android:id="@+id/switchWidget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:clickable="true"
        android:focusable="false"
        android:padding="8dip" 
        android:layout_marginRight="14dip"/>

</merge>

在该onPostCreate方法中,我试图在运行时使用findViewById(R.id.switchWidget)但它始终返回 null 来查找此复选框/开关。

知道可能是什么原因吗?

4

2 回答 2

7

原因是我在屏幕上添加了多个首选项,因此,我不得不求助于添加自定义首选项。详细解决方案已在以下链接中提供:

解决方案已在以下链接中提供。

在偏好活动中使用 findviewbyid

于 2013-04-02T12:03:45.320 回答
1

原因在于使用了几个PreferenceScreen。每个 Preference 视图都有自己的 id。

要访问视图 ID,您需要创建自己的 Preference 类,继承自 Preference 并覆盖 onBindView

public class LogListPreference extends EditTextPreference {

    public LogListPreference(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LogListPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setLayoutResource(R.layout.preference_widget_loglist);
    }

     @Override
        protected void onBindView(View view) {
            super.onBindView(view);
            EditText edit = (EditText) view.findViewById(R.id.logText);
            ...
        }
}

要在 PreferenceScreen xml 文件中使用您的类,请指定

 <PreferenceScreen
 xmlns:android="http://schemas.android.com/apk/res/android">

     <com.company.LogListPreference
         android:title="@string/pref_log_title_log"
         android:key="log_text" />

 </PreferenceScreen>

首选项布局文件/layout/preference_widget_loglist.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- Layout used by CheckBoxPreference for the checkbox style. This is inflated
     inside android.R.layout.preference. -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget_log_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="top|left"
    android:orientation="vertical"
    >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
        android:paddingRight="?android:attr/listPreferredItemPaddingRight">
        >

        <TextView
            android:id="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceListItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView
            android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@android:id/title"
            android:layout_alignStart="@android:id/title"
            android:layout_below="@android:id/title"
            android:maxLines="2"
            android:paddingTop="6dp"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:textSize="14sp" />


    </RelativeLayout>


    <EditText
        android:id="@+id/logText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="false"
        android:defaultValue="@string/pref_default_video_sources"
        android:focusable="false"
        android:gravity="top|left"
        android:inputType="textMultiLine"
        android:key="log331"
        android:lines="10"
        android:maxLines="2000"
        android:minLines="6"
        android:scrollbars="vertical"
        android:scrollbarThumbVertical="?android:attr/colorBackground"
        android:background="?android:attr/colorForeground"
        android:textColor="?android:attr/textColorPrimaryInverse"
        android:text="aaa\nbbb\nccc\naaa\nbbb"
        android:title="@string/pref_title_video_sources" />

</LinearLayout>
于 2018-06-19T05:56:43.740 回答