如何将button添加到PreferenceScreen

有什么方法可以在首选项屏幕的底部添加一个button,并在滚动时使它们正常工作?

还有另一个解决scheme来定制偏好的外观。

使用button或任何想要添加到标准首选项的devise来devise正常的XML布局。 在你的布局中包含一个ListView ,并给它ID @android:id/list

比方说,我们调用布局文件res/layout/main.xml 。 它可能看起来像这样:

 <?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="fill_parent" android:orientation="vertical"> <Button android:text="This is a button on top of all preferences." android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> 

在您的PreferenceActivity ,将这两行添加到您的onCreate

 addPreferencesFromResource(R.xml.preferences); setContentView(R.layout.main); 

然后,您的布局中的ListView将被res/xml/preferences.xml常用方法中定义的首选项replace。

我知道这有点晚了,但我刚刚find一个解决scheme,我比马克斯的赞美的解决scheme更好。

您可以简单地添加一个页脚(或者如果您喜欢button在顶部,一个标题)到PreferenceActivity的ListView像这样:

 public class MyActivity extends PreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); ListView v = getListView(); v.addFooterView(new Button(this)); } } 

我希望这可以帮助别人。

下面的这个例子将在页面底部呈现一个button(如果有人仍然感兴趣)。

在LinearLayout的情况下,你也可以应用权重; 这是需要的,因为Listview被设置为* fill_parent *。 我通常通过添加* android:layout_weight *来实现:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="10"/> <Button android:text="This is a button on top of all preferences." android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"/> </LinearLayout> 

下面的解释不是100%,但它会帮助你理解…

 +-- View Port (linear layout) | +-- List View (this is where the preferences will go) | | | | | +-- +-- +-- | Button (which was pushed out of view by the fillparent of ListView +-- 

你也可以这样说,因为Button没有重量; 该button呈现在0dp高度。

现在随着layout_weigths的增加,它将把button呈现在视图中

 +-- View Port (linear layout) | +-- List View (this is where the preferences will go) | | | | | +-- | +-- | | Button (which was pushed out of view by the fillparent of ListView | +-- +-- 

事实上,有一个解决scheme。 这里是一个代码,我希望这对任何人都有用。 它看起来像屏幕下方的3个选项和2个button,与屏幕分辨率无关(最低定位为240)

 package com.myapplication.gui; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceScreen; import android.view.Display; import android.view.Gravity; import android.view.WindowManager; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.ScrollView; import com.myproject.general.HeightListView; import com.myapplication.R; public class FilterActivity extends PreferenceActivity { private LinearLayout rootView; private LinearLayout buttonView; private Button buttonDone; private Button buttonRevert; private ListView preferenceView; private LinearLayout gradientView; private ScrollView scrollRoot; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); int height = display.getHeight(); int width = height > 240 ? display.getWidth() : display.getWidth() - 4; scrollRoot = new ScrollView(this); scrollRoot.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); rootView = new LinearLayout(this); rootView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); rootView.setOrientation(LinearLayout.VERTICAL); buttonView = new LinearLayout(this); buttonView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); buttonView.setOrientation(LinearLayout.HORIZONTAL); buttonView.setGravity(Gravity.BOTTOM); gradientView = new LinearLayout(this); gradientView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); gradientView.setOrientation(LinearLayout.HORIZONTAL); gradientView.setBackgroundResource(R.drawable.gradient); gradientView.setPadding(0, 5, 0, 0); gradientView.setBackgroundResource(R.drawable.gradient); buttonDone = new Button(this); buttonDone.setText(R.string.filterButton_Done); buttonDone.setLayoutParams(new LayoutParams(width/2, LayoutParams.WRAP_CONTENT)); gradientView.addView(buttonDone); buttonRevert = new Button(this); buttonRevert.setText(R.string.filterButton_Revert); buttonRevert.setLayoutParams(new LayoutParams(width/2, LayoutParams.WRAP_CONTENT)); gradientView.addView(buttonRevert); buttonView.addView(gradientView); preferenceView = new HeightListView(this); preferenceView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); preferenceView.setId(android.R.id.list); PreferenceScreen screen = createPreferenceHierarchy(); screen.bind(preferenceView); preferenceView.setAdapter(screen.getRootAdapter()); rootView.addView(preferenceView); rootView.addView(buttonView); if (height > 240) { this.setContentView(rootView); } else { scrollRoot.addView(rootView); this.setContentView(scrollRoot); } setPreferenceScreen(screen); } private PreferenceScreen createPreferenceHierarchy() { PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this); PreferenceScreen pref1 = getPreferenceManager().createPreferenceScreen(this); pref1.setKey("pref1"); pref1.setTitle("Title"); pref1.setSummary("Summary"); root.addPreference(pref1); PreferenceScreen pref2 = getPreferenceManager().createPreferenceScreen(this); pref2.setKey("pref2"); pref2.setTitle("Title"); pref2.setSummary("Summary"); root.addPreference(pref2); PreferenceScreen pref3 = getPreferenceManager().createPreferenceScreen(this); pref3.setKey("pref3"); pref3.setTitle("Title"); pref3.setSummary("Summary"); root.addPreference(pref3); return root; } } 

这就是代码在ronny示例中的活动中的样子。 我的意图是在屏幕的底部放置一个菜单。

 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.prefs); addPreferencesFromResource(R.xml.prefs); /* LayoutInflater CX = getLayoutInflater(); CX.inflate(R.layout.main,null);*/ // TODO Auto-generated method stub } 
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="@dimens/listview_height" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="This is a button on top of all preferences." /> </RelativeLayout> 

我引用@Ronnie,使用RelativeLayout并为listview的layout_height设置一个高度,然后设置button的layout_alignParentBottom =“true”,它可以在PreferenceScreen底部渲染一个button; 然后使用@Max的方式。 它适用于我的需求。

也可以将操作button添加到android标准方法的操作栏中。

 public class PrefActivity extends PreferenceActivity{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu items for use in the action bar MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.preference_header_menu, menu); return super.onCreateOptionsMenu(menu); } } <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/action_add" android:icon="@drawable/ic_menu_add_dark" android:title="@string/menu_action_add_title" android:showAsAction="always" /> </menu> 

您只需在普通的Activity中使用PreferenceFragment,并将该button添加到活动布局中即可。

 public class SettingActivity extends Activity { UserProfileViewModel userProfileViewModel = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_setting); getFragmentManager().beginTransaction() .replace(R.id.content, new SettingsFragment()) .commit(); } private class SettingsFragment extends PreferenceFragment { public SettingsFragment() { } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.pref_main); } } } 

SettingActivity.java

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/buttonSave"/> <Button android:id="@+id/buttonSave" android:text="Save" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout> 

activity_setting

在这里输入图像描述

首选项活动中的自定义视图有助于在Android中的PreferenceActivity中添加自定义视图。

创buildmain.xml,唯一必要的视图是一个ListView,id: android:id="@android:id/list"

 <?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" android:weightSum="1"> <ListView android:id="@android:id/list" android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="0dp"> </ListView> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> 

创buildCustomPreferenceActivity.java

 public class CustomPreferenceActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); addPreferencesFromResource(R.xml.settings); //setup any other views that you have TextView textView = (TextView) findViewById(R.id.textView); textView.setText("View Added"); } } 

以下是一个简单的解决scheme,将一个可点击的button添加到您的首选项屏幕。 这很容易,因为首选项已经保留了android:widgetLayout中的空间,并且button可以通过android:onClick传递点击。

首先用内容创build一个button.xml

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:text="BUTTON" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/button" android:onClick="onButtonClick"/> </LinearLayout> 

现在在你的preferences.xml中,添加首选项

 <Preference android:key="button" android:title="Title" android:summary="Summary" android:widgetLayout="@layout/button" /> 

您的PreferenceActivity现在只需要包含一个onButtonClick成员

 public class MainActivity extends PreferenceActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.main_preferences); } public void onButtonClick(View v) { Log.d("Button", "Yeah, button was clicked"); } }