首选项隐藏工具栏

时间:2015-04-19 09:58:39

标签: java android xml toolbar

解决https://stackoverflow.com/posts/26564401/revisions

我的偏好设置屏幕出现问题。如果有人知道这个问题。我测试了很多东西,但我真的不了解偏好片段的布局......

Activity_settings.xml

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    app:navigationContentDescription="@string/abc_action_bar_up_description"
    android:background="?attr/colorPrimary"
    app:navigationIcon="?attr/homeAsUpIndicator"
    app:title="@string/action_settings"
    />

的preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout="@layout/activity_settings">
    <EditTextPreference android:title="Your Name"
        android:key="username"
        android:summary="Please provide your username"></EditTextPreference>
    <CheckBoxPreference android:title="Application Updates"
        android:defaultValue="false"
        android:summary="This option if selected will allow the application to check for latest versions."
        android:key="applicationUpdates" />
    <ListPreference     android:title="Download Details"
        android:summary="Select the kind of data that you would like to download"
        android:key="downloadType"
        android:defaultValue="1"
        android:entries="@array/listArray"
        android:entryValues="@array/listValues" />
    <PreferenceScreen
        android:title="Test de merde"
        android:summary="Lolilol de merde">

        <intent android:action="android.intent.action.VIEW"
            android:data="http://www.android.com" />

    </PreferenceScreen>
    <PreferenceScreen
        android:title="Bonjour"
        android:summary="Hello"
        android:key="user">
    </PreferenceScreen>
</PreferenceScreen>

MyPreferencesActivity.java

package activity;

import android.app.FragmentManager;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.pierre.tan.R;

/**
 * Created by dev on 19/04/15.
 */
public class MyPreferencesActivity extends PreferenceActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
            Toolbar bar;

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                LinearLayout root = (LinearLayout) findViewById(android.R.id.list).getParent().getParent().getParent();
                bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.activity_settings, root, false);
                root.addView(bar, 0); // insert at top
            } else {
                ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
                ListView content = (ListView) root.getChildAt(0);

                root.removeAllViews();

                bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.activity_settings, root, false);


                int height;
                TypedValue tv = new TypedValue();
                if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
                    height = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
                }else{
                    height = bar.getHeight();
                }

                content.setPadding(0, height, 0, 0);

                root.addView(content);
                root.addView(bar);
            }

            bar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }






    public static class MyPreferenceFragment extends PreferenceFragment
        {
            @Override
            public void onCreate(final Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);

                addPreferencesFromResource(R.xml.preferences);

                Preference userButton = (Preference) findPreference("user");
                userButton.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                    @Override
                    public boolean onPreferenceClick(Preference arg0) {
                        Intent intent = new Intent(getActivity(), ChangelogActivity.class);
                        startActivity(intent);
                        return true;
                    }
                });
            }
        }

    }

截图显示我的问题:

enter image description here

谢谢大家:)

1 个答案:

答案 0 :(得分:0)

使用setSupportActionBar(bar)而不是root.addView(bar)