相关疑难解决方法(0)

自定义PreferenceScreen的布局

我的要求

注意:我需要支持Android API 15及更高版本.

在我的PreferenceFragment中,我正在将PreferenceScreen动态添加到PreferenceCategory.

PreferenceScreen as = mgr.createPreferenceScreen(context);
as.setTitle(name); 
myCategory.addPreference(as);
Run Code Online (Sandbox Code Playgroud)

当设置活动呈现时,这些PreferenceScreens仅在其行的标题中呈现,请参见下图. PreferenceScreen在设置活动中显示

我想自定义此行中显示的内容.理想情况下,我希望在下面有一个摘要标题,标题/摘要左侧的图标和右侧的某些行上的图标.请参阅下面的模型图像.

期望的我的偏好屏幕的渲染

PreferenceScreen.setWidgetLayoutResource(int widgetLayoutResId)

我知道我可以使用"setWidgetLayoutResource"在我的设置活动中使用以下代码在行布局的右侧添加一个图标(以及带有"setSummary"的摘要)

PreferenceScreen as = mgr.createPreferenceScreen(context);
as.setTitle(name); 
as.setSummary(summary);
as.setWidgetLayoutResource(R.layout.as_widget);
myCategory.addPreference(as);
Run Code Online (Sandbox Code Playgroud)

其中"as_widget.xml"是

<?xml version="1.0" encoding="utf-8"?>
<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_action_favorite"/>
Run Code Online (Sandbox Code Playgroud)

这将产生如下的UI 使用setWidgetLayoutResource进行布局

这让我更接近我想要的但仍然不完全是我想要的(错过了行开头的图标).

PreferenceScreen.setLayoutResource(int layoutResId)

我相信如果我使用它,那么我可以控制整行的渲染.我在stackoverflow上看过这个例子,比如

根据我的理解,您指定的布局文件必须具有以下内容

  • 它的根视图的id为"@android:id/widget_frame"
  • 一个id为android的视图:id ="@ + android:id/title"(这是放置PreferenceScreen.setTitle中指定的字符串的地方)
  • id为android的视图:id ="@ + android:id/summary"(这是放置PreferenceScreen.setSummary中指定的字符串的位置)

然而,当我尝试这样做时,Android Studio突出显示"@ + android:id/summary",错误为"无法解析符号'@ + android:id/summary'.当应用程序运行时,我的行呈现为完全空白.

我的java是

PreferenceScreen as = mgr.createPreferenceScreen(context);
as.setTitle(name); 
as.setSummary(summary);
as.setLayoutResource(R.layout.as_row_layout);
myCategory.addPreference(as);
Run Code Online (Sandbox Code Playgroud)

我的布局是

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/widget_frame"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingRight="?android:attr/scrollbarSize">

    <ImageView
        android:id="@+id/icon1"
        android:src="@drawable/ic_action_email" …
Run Code Online (Sandbox Code Playgroud)

layout android customizing preference preferencescreen

11
推荐指数
1
解决办法
1万
查看次数

如何使用android:layout属性实例化自定义首选项的布局

我可以通过android:layout属性为首选项设置适当的布局.举个例子

<Preference
  android:key="friction" 
  android:title="@string/friction" 
  android:layout="@layout/friction_fragment" 
  android:shouldDisableView="true" 
  android:defaultValue="30" 
  android:enabled="true"
  android:selectable="true" 
  android:summary="Bite friction">
</Preference>
Run Code Online (Sandbox Code Playgroud)

布局在哪里

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:text="@string/friction" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"></TextView>
    <SeekBar android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/sbFriction"></SeekBar>
    <TextView android:text="@string/friction_little" android:id="@+id/txtSummary" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <Button android:text="Button" android:id="@+id/btnFriction" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

我可以在PreferenceActivity中获取OnCreate中的视图

    Preference fric = (Preference)this.findPreference("friction");
    View v = fric.getView(null, null);
    SeekBar sbFriction = (SeekBar)v.findViewById(R.id.sbFriction);
    sbFriction.setOnSeekBarChangeListener(this);
    Button btnFric = (Button) v.findViewById(R.id.btnFriction);
    btnFric.setOnClickListener(m_onClick);
Run Code Online (Sandbox Code Playgroud)

但是我设定的这些事件听众并没有被解雇.我如何捕获这些事件,例如 - 单击按钮.编辑.不,它没有发动任何异常.这是更详细的代码

public class SettingsActivity extends PreferenceActivity implements OnPreferenceChangeListener, OnSeekBarChangeListener
{

    private TextView …
Run Code Online (Sandbox Code Playgroud)

layout android preference

9
推荐指数
1
解决办法
6690
查看次数