Android PreferenceFragment中的操作栏未重新计算高度和字体大小

Android PreferenceFragment中的操作栏未重新计算高度和字体大小,android,android-actionbar,android-orientation,preferenceactivity,android-appcompat,Android,Android Actionbar,Android Orientation,Preferenceactivity,Android Appcompat,使用最新的AppCompat-v21库,我使用ActionBarActivity创建并填充PreferenceFragment。但是,当方向或屏幕大小改变时,操作栏似乎不会改变高度和文本大小。在其他活动中也测试这一点,这种行为似乎只发生在首选活动中(与此处提出的问题相反:) 首先,为了处理方向更改,我在清单中添加了android:configChanges=“keyboard | keyboardHidden | orientation | screenSize”。我怀疑这是造成此问题的主要原因

使用最新的
AppCompat-v21
库,我使用
ActionBarActivity
创建并填充
PreferenceFragment
。但是,当方向或屏幕大小改变时,
操作栏
似乎不会改变高度和文本大小。在其他活动中也测试这一点,这种行为似乎只发生在
首选活动中(与此处提出的问题相反:)

首先,为了处理方向更改,我在清单中添加了
android:configChanges=“keyboard | keyboardHidden | orientation | screenSize”
。我怀疑这是造成此问题的主要原因,但正如我前面提到的,这适用于其他
活动

以下是一些解释问题的屏幕截图:

在纵向模式下启动了首选项活动

从纵向旋转到横向:

在横向模式下启动
首选项活动

从横向旋转到纵向:

其他信息

以下是
首选项活动
课程:

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

public class PrefsActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefsFragment()).commit();
    }

}
这种行为是错误吗?如果没有,是否有解决方法或修复方法?
编辑I

我尝试使用新的
工具栏
小部件,但没有成功

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;

public class PrefsActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_preference);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_pref);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        getFragmentManager().beginTransaction().replace(R.id.pref_frame, new PrefsFragment()).commit();
    }

}

您测试的其他活动是否也在清单上声明了
android:configChanges

正如你所说,问题在于

android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
不处理旋转。它实际上停止了活动和视图的重新创建,导致视图在配置更改后无法读取相应的值


考虑到您正在使用片段,我将删除
android:configChanges
属性,并让
PrefsFragment
保留它的实例。

android:configChanges
不建议使用,因为方向、键盘不是活动的唯一原因

因此,更好的方法是从清单中删除它,并设置
setRetainInstance(true)
片段
类上。这将绕过片段的销毁和重新创建循环,同时刷新活动

由于
工具栏
包含在活动中,而不是片段中,因此这将刷新工具栏,同时保留片段

对于对话框,可以使用关闭/重新打开对话框


来源:

我可以提供这个解决方案

与系统操作栏、棒棒糖工具栏和AppCompat工具栏配合使用

public static void fixActionBar(Activity activity, View toolbar)
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
    {
        if (toolbar == null)
        {
            int id = activity.getResources().getIdentifier("action_bar", "id", "android");
            if (id != 0) toolbar = (Toolbar) activity.findViewById(id);
        }
        if (toolbar != null)
        {
            Context context = toolbar.getContext();
            TypedArray typedArray = context.obtainStyledAttributes(new int[]
                    {android.R.attr.actionBarStyle, android.R.attr.actionBarSize});
            int actionStyle = typedArray.getResourceId(0, 0);
            int actionHeight = typedArray.getDimensionPixelSize(1, 0);
            typedArray.recycle();
            try
            {
                View container = (View) toolbar.getParent();
                Field field = container.getClass().getDeclaredField("mHeight");
                field.setAccessible(true);
                field.setInt(container, actionHeight);
                View overlay = (View) container.getParent();
                field = overlay.getClass().getDeclaredField("mActionBarHeight");
                field.setAccessible(true);
                field.setInt(overlay, actionHeight);
            }
            catch (Exception e)
            {

            }
            toolbar.getLayoutParams().height = actionHeight;
            toolbar.setMinimumHeight(actionHeight);
            typedArray = context.obtainStyledAttributes(actionStyle, new int []
                    {android.R.attr.titleTextStyle});
            ((Toolbar) toolbar).setTitleTextAppearance(context, typedArray.getResourceId(0, 0));
            typedArray.recycle();
        }
    }
}

从活动的OnConfiguration Changed中调用此方法。

这里也有同样的问题。但在我的例子中,横向字体大小太小,无论它是如何开始的……您是否尝试在styles.xml中定义
textSize
?有时,当您没有定义一个确定的值时,android会出现错误。你可以读这个:和