java - PreferenceActivity 中的工具栏设置

标签 java android android-toolbar preferenceactivity

我正在尝试使用以下代码在首选项 Activity 中设置工具栏。它确实显示了工具栏,但它覆盖了首选项列表。 Here来 self 的应用程序的屏幕截图链接 我正在做这个 bcz 我正在运行时更改样式主题。我已经在其他 Activity 中实现了它,但我也希望它适用于 PreferenceActivity。

设置 Activity

    public class SettingsActivity extends PreferenceActivity
{
    private AppCompatDelegate mDelegate;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {


        getDelegate().installViewFactory();
        getDelegate().onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        setSupportActionBar((Toolbar) findViewById(R.id.toolbarsettings));
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPrefernceFragment()).commit();

    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {


        if (item.getItemId() == android.R.id.home)
        {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed()
    {
        finish();
        super.onBackPressed();
    }






    public static class MainPrefernceFragment extends PreferenceFragment
    {

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

        }

    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState)
    {
        super.onPostCreate(savedInstanceState);
        getDelegate().onPostCreate(savedInstanceState);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID)
    {
        getDelegate().setContentView(layoutResID);
    }

    @Override
    protected void onPostResume()
    {
        super.onPostResume();
        getDelegate().onPostResume();
    }

    @Override
    protected void onStop()
    {
        super.onStop();
        getDelegate().onStop();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        getDelegate().onDestroy();
    }

    private void setSupportActionBar(@Nullable Toolbar toolbar)
    {
        getDelegate().setSupportActionBar(toolbar);
    }

    private AppCompatDelegate getDelegate()
    {
        if (mDelegate == null)
        {
            mDelegate = AppCompatDelegate.create(this, null);
        }
        return mDelegate;
    }

}

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content_frame"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarsettings"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        />
    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

最佳答案

我认为它会解决你的问题:

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity" >

<android.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbarsettings"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

</android.support.design.widget.AppBarLayout>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ListView
        android:id="@android:id/list"
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
    </ListView>
</LinearLayout>

和java代码:

 package com.matingdz.myapplication;

import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;


import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends PreferenceActivity
{
    private AppCompatDelegate mDelegate;


    ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {


        getDelegate().installViewFactory();
        getDelegate().onCreate(savedInstanceState);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setSupportActionBar((Toolbar) findViewById(R.id.toolbarsettings));
        getFragmentManager().beginTransaction().replace(android.R.id.content, new MainPrefernceFragment()).commit();

        String[] x = new String[]{"AAA","BBB","CCC"};
        ListView lv = (ListView) findViewById(android.R.id.list);
        ArrayAdapter<String> test = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1,x);
        lv.setAdapter(test);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {


        if (item.getItemId() == android.R.id.home)
        {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed()
    {
        finish();
        super.onBackPressed();
    }


    public static class MainPrefernceFragment extends PreferenceFragment
    {

    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState)
    {
        super.onPostCreate(savedInstanceState);
        getDelegate().onPostCreate(savedInstanceState);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID)
    {
        getDelegate().setContentView(layoutResID);
    }

    @Override
    protected void onPostResume()
    {
        super.onPostResume();
        getDelegate().onPostResume();
    }

    @Override
    protected void onStop()
    {
        super.onStop();
        getDelegate().onStop();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        getDelegate().onDestroy();
    }

    private void setSupportActionBar(@Nullable Toolbar toolbar)
    {
        getDelegate().setSupportActionBar(toolbar);
    }

    private AppCompatDelegate getDelegate()
    {
        if (mDelegate == null)
        {
            mDelegate = AppCompatDelegate.create(this, null);
        }
        return mDelegate;
    }

}

关于java - PreferenceActivity 中的工具栏设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54167332/

相关文章:

java - 尝试针对 xsd 文件验证 xml 时未找到文件异常

android - 使用 Chrome 浏览器在 Android 上搜索输入文本字段

java - 在 xml View 中使用 Selenium 1.82 By.xpath 会出现 NoSuchElementException

java - 在标签库描述符中使用可变参数

java - 将 HAPI-FHIR 依赖项添加到 gradle Android 项目中

android - longClick 监听器的持续时间

android - 在谷歌地图上绘制虚线折线 - Android

android - CollapsingToolbar 未完全折叠

android - 工具栏不会在透明状态栏下滚动

android - 工具栏 : IllegalStateException - configure your build for VectorDrawableCompat