在preferenceScreen中加入自己设计的layout布局

本文来自:点击打开链接

图1中上面的listtitle是一个listPreference,当你点击后会出现图2的效果,然后在图2中选择ABC其中一个,这个dialog会消失,并将选择的文本显示在图1中

而下面的部分是在PreferenceScreen中嵌套一个PreferenceScreen,在内部的PreferenceScreen中使用android:@layout/your_layout将你自定义的layout加
入到内部的PreferenceScreen中,贴代码:
1、PreferenceScreen :
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="using_categories_in_root_screen"
    android:summary="Using Preference Categories"
    android:title="Categories" >
   
    <ListPreference
        android:defaultValue="@string/manual_check"
        android:dialogTitle="@string/make_choice"
        android:entries="@array/frequency_choice"
        android:entryValues="@array/frequency_choice"
        android:key="list"
        android:title="@string/check_frequency" />
    <PreferenceCategory >
        <PreferenceScreen
            android:title="layut"
            android:key="layout"
            android:layout="@layout/your_activity_main" >
        </PreferenceScreen>
    </PreferenceCategory>
</PreferenceScreen>

2、your_layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:clickable="true"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingTop="100dp"
        android:paddingLeft="50dp"
        android:paddingBottom="100dp" >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/recently_check_time"
            android:textSize="24sp" />
        <TextView
            android:id="@+id/lastupdatetime_view"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/nothing" />
    </LinearLayout>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <Button
            android:id="@+id/btn_manualcheck"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="30dp"
            android:layout_marginRight="20dp"
            android:text="@string/now_check" />
    </RelativeLayout>
</LinearLayout>
3、java:
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  addPreferencesFromResource(R.xml.frequency_check_time);
  
  b_manualCheck = (Button) findViewById(R.id.btn_manualcheck);
  b_manualCheck.setOnClickListener(new OnClickListenerImpl());
  Preference listPreference = findPreference("list");
  listPreference.setOnPreferenceChangeListener(this);
  listPreference.setOnPreferenceClickListener(this);
  String summary = PreferenceManager.getDefaultSharedPreferences(this)
    .getString(listPreference.getKey(), "@string/manual_check");
  listPreference.setSummary(summary);
}

private class OnClickListenerImpl implements OnClickListener{
  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   
  }
  
}
@Override
public boolean onPreferenceClick(Preference preference) {
  
  return true;
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
  preference.setSummary((String) newValue);
  return true;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值