Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何将按钮添加到首选项屏幕_Android_Android Preferences - Fatal编程技术网

Android 如何将按钮添加到首选项屏幕

Android 如何将按钮添加到首选项屏幕,android,android-preferences,Android,Android Preferences,有没有办法在“首选项”屏幕的底部添加一个按钮,使其在滚动时正常工作?实际上,有一个解决方案。这里是一个代码,我希望,这将是有用的任何人。 它看起来像屏幕底部的3个选项和2个按钮,与屏幕分辨率无关(目标最低为240) 还有另一种自定义首选项外观的解决方案 设计一个普通的XML布局,包括按钮或任何您想添加到标准首选项中的内容。在布局中包含一个列表视图,并将ID设置为@android:ID/list <?xml version="1.0" encoding="utf-8"?> <Li

有没有办法在“首选项”屏幕的底部添加一个按钮,使其在滚动时正常工作?

实际上,有一个解决方案。这里是一个代码,我希望,这将是有用的任何人。 它看起来像屏幕底部的3个选项和2个按钮,与屏幕分辨率无关(目标最低为240)


还有另一种自定义首选项外观的解决方案

设计一个普通的XML布局,包括按钮或任何您想添加到标准首选项中的内容。在布局中包含一个
列表视图
,并将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>
假设我们调用布局文件
res/layout/main.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"
              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="match_parent"
              android:layout_height="wrap_content" />
</LinearLayout>

布局中的
列表视图
将被
res/xml/preferences.xml
中通常定义的首选项所取代。下面的示例将在页面底部显示一个按钮(以防有人仍然感兴趣)

在线性布局的情况下,您也可以应用权重;这是必需的,因为Listview设置为*fill\u parent*。 我通常通过添加*android:layou\u 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>
你也可以说,因为按钮没有重量;按钮以0dp高度渲染

现在,随着布局的增加,它将在视图中显示“渲染”按钮

+-- 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
| +--
+--

这就是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
}

我知道这有点晚了,但我刚刚找到了一个比Max称赞的解决方案更喜欢的解决方案

您只需在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));
    }
}
我希望这对某人有所帮助。


 <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的布局设置高度,然后设置按钮的布局\u alignParentBottom=“true”,它可以在PreferenceScreen的底部呈现一个按钮;
然后使用@Max的方式。它可以满足我的需要。

也可以为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>
公共类PrefActivity扩展了PreferenceActivity{
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单项充气,以便在操作栏中使用
MenuInflater充气机=getMenuInflater();
充气机。充气(右菜单。首选项\标题\菜单,菜单);
返回super.onCreateOptions菜单(菜单);
}
}
这将有助于在Android的PreferenceActivity中添加自定义视图

创建main.xml,唯一需要的视图是ListView,id:
android:id=“@android:id/list”


下面是一个简单的解决方案,可以在首选项屏幕中添加一个可单击的按钮。这很容易,因为首选项已经在android:widgetLayout中保留了空间,并且按钮可以通过android:onClick传递单击

首先创建一个包含内容的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>

您只需要在常规活动中使用PreferenceFragment,并将按钮添加到活动布局中

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);

        }
    }
}
设置Activity.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>

活动设置

preferences.xml:

    <Preference
        android:key="clearAllData"
        android:title="@string/settings_clear_all_data">
    </Preference>

我发现上面的所有答案都不可用,因为我创建的任何布局都无法在自定义布局中“包装”
首选项屏幕
容器(然后在
列表视图
下添加一个按钮)

它们仅覆盖首选项列表顶部的自定义布局(浮动),单击(例如)新的自定义按钮将仅调用按钮下方的首选项



但是,我发现在使用
PreferenceFragment

时,在首选项列表容器下方添加按钮时,哪一个更有效?您是否考虑过为未来的访问者创建自定义首选项?而Max的解决方案有效,还有一个替代的解决方案:抱歉,错过了@StealthCopter的帖子,当按钮放在列表视图下面时,它不起作用。当preferences活动使用对话框主题时不起作用。关于使用PreferenceActivity的最新文档提到,对于所有新的开发PreferenceFragment是首选的实现方式。似乎给定的解决方案在这种情况下不起作用。它仍然不能随列表滚动。我有点困惑于这个答案有多少选票,因为它实际上没有回答原始问题。它不会随列表滚动,当按钮放在视图底部时也不起作用,如上面的注释所述。@howettl只需将fill\u parent更改为在ListView布局中包装内容。\u Heights至少作为Max的解决方案,它感觉更好,因为它不依赖元素id。让我们看看它在Android未来版本中的表现…@DanielF。不到桥就不要过桥;)我无法使用PreferenceFragment获得
列表视图(不,它不会那样工作(因为
PreferenceFragment
有自己的列表),但我只通过创建
首选项
并在其上设置
意图
来解决它。不需要创建按钮(至少在我的情况下是这样)不适用于PreferenceFragmentCompat,因为getListView实际上会返回一个RecycleService这是一个不错的解决方案,但您忘记了将listview设置在按钮上方。目前,listview的项目可能位于按钮后面,这是不推荐的,因为如果它是最后一个项目,它将永远不可见,因此无法单击。哦,是的s、 你是对的,我忘记了这个例子,因为我的listview只有5项,谢谢。请更新你的答案,这样其他人就不会有这种行为。我想你也忘记了一些属性和relativeLayout的结束标记。事实上,设置正确的listview_高度可以解决这个问题
<?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>
<Preference
    android:key="button"
    android:title="Title"
    android:summary="Summary"
    android:widgetLayout="@layout/button" />
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");
}
}
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);

        }
    }
}
<?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>
    <Preference
        android:key="clearAllData"
        android:title="@string/settings_clear_all_data">
    </Preference>
public class SettingsFragment extends PreferenceFragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);

        Preference clearAllData = (Preference) findPreference("clearAllData");

        // setup buttons
        final Context context = getActivity();
        clearAllData.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

            @Override
            public boolean onPreferenceClick(Preference preference) {
                ...
            }
    }

}