gpt4 book ai didi

java - PreferenceActivity 中的工具栏设置

转载 作者:行者123 更新时间:2023-11-30 12:08:51 25 4
gpt4 key购买 nike

我正在尝试使用以下代码在首选项 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com