标签: onconfigurationchanged

layout-land xml文件不能与onConfigurationChanged回调一起使用

我有纵向和横向模式的不同布局,我还需要覆盖 onConfigurationChanged()回调.但问题是,当我将手机方向更改为横向时,我的横向布局不起作用.

任何人都可以告诉我这个onConfigurationChanged回叫问题或其他原因造成的吗?

任何帮助都将是值得赞赏的.

layout android onconfigurationchanged

3
推荐指数
1
解决办法
6708
查看次数

为什么不调用 onConfigurationChanged?

我正在编写一个代码,在更改方向时更改 TextView 的文本我正在使用 onConfigurationChanged() 侦听器来更改文本

override fun onConfigurationChanged(newConfig: Configuration?) {
    super.onConfigurationChanged(newConfig)

    val orientation : Int = getResources().getConfiguration().orientation
    val oTag = "Orientation Change"

    if(orientation == (Configuration.ORIENTATION_LANDSCAPE)){
        mytext?.setText("Changed to Landscape")
        Log.i(oTag, "Orientation Changed to Landscape")

    }else if (orientation == (Configuration.ORIENTATION_PORTRAIT)){
        mytext?.setText("Changed to Portratit")
        Log.i(oTag, "Orientation Changed to Portratit")
    }else{
        Log.i(oTag, "Nothing is coming!!")
    }
}
Run Code Online (Sandbox Code Playgroud)

但即使在日志中也没有任何反应

我也在清单文件中将此属性添加到我的活动中

android:configChanges="orientation"
Run Code Online (Sandbox Code Playgroud)

我在这里错过了什么?还有其他方法可以实现这一目标吗?

android orientation onconfigurationchanged kotlin

3
推荐指数
1
解决办法
3520
查看次数

在方向更改时未调用onConfigurationChanged()

我在SO上看到了一些相关问题,但我无法解决我的问题.我为PORTRAIT和LANDSCAPE模式创建了单独的布局.

我已经修改了AndroidManifest.xml文件以进行相应的方向更改,但是onConfigurationChanged()当我在活动中实现它时,它不起作用.

现在问题出在layout.addView(graphView, lp);onCreate().我在GraphView课堂上写了绝对的硬编码值.

所以它完全适用于PORTRAIT模式,但是当我切换到LANDSCAPE模式时,graphView没有正确放置.

为了解决这个问题,我创建了GraphViewLand专门为LANDSCAPE模式编码的类.但它并没有被召唤出来onConfigurationChanged().

其余的布局完美无缺,因为我main.xml为每个方向创建了单独的文件.但由于graphView是以编程方式创建的,因此未正确放置.

我在这里做错了吗?

我刚从这里读到这个:

对于任何类型的配置更改,您说您在那里处理,您将收到对当前活动的onConfigurationChanged(配置)方法的调用,而不是重新启动.但是,如果配置更改涉及您未处理的任何内容,则仍将重新启动活动并且不会调用onConfigurationChanged(Configuration).

onCreate()方法

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    init();

    RelativeLayout layout = (RelativeLayout) findViewById(R.id.MainLayout);

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.ALIGN_TOP, btnindex.getId());

    GraphView graphView = new GraphView(this, values, "CurrentGraph",
                horlabels, verlabels);
    layout.addView(graphView, lp);
    }
Run Code Online (Sandbox Code Playgroud)

onConfigurationChanged()方法

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", …
Run Code Online (Sandbox Code Playgroud)

java android orientation relativelayout onconfigurationchanged

2
推荐指数
1
解决办法
4537
查看次数

旋转设备时EditText数据丢失

伙计们,我完成了它.我一直在谷歌搜索过去2天,但我找不到确切的解决方案.每个人都在谈论configChanges和所有那些在我的案例中似乎不起作用的陈词滥调.

我有一个登录屏幕,由2个EditTexts组成.现在,此登录屏幕具有不同的纵向和横向布局.所以我不得不在layout文件夹中创建一个login.xml,在layout-land文件夹中创建另一个login.xml.为了支持方向更改,我在LoginActivity类中覆盖了onConfigurationChanged()方法.在这个方法中,我调用setContentView(R.layout.login)方法,以便将适当的login.xml设置为每个方向的布局.

毕竟我在我的清单文件中定义了以下内容:

android:configChanges="orientation|keyboardHidden"
Run Code Online (Sandbox Code Playgroud)

但我仍然面临着着名的老问题.如果Edittext中有任何文本并且设备已旋转,则该文本将丢失.我不想丢失那个文字.有可能吗?我已经读过我们可以使用onSaveInstanceState(Bundle savedInstanceState)方法来做到这一点,我甚至尝试了它,但它对我不起作用.请帮忙.提前致谢.

android orientation onconfigurationchanged android-edittext

2
推荐指数
1
解决办法
5489
查看次数

对象隐藏在方向更改上

我想隐藏一些关于方向改变的元素(LANDSCAPE).我把它放在哪里?

Button bt0 = (Button) findViewById(R.id.button0);
bt0.setVisibility(bt0.GONE);
Run Code Online (Sandbox Code Playgroud)

然后,让他们回归PORTRAIT.它们已在"创建"中定义.

android onconfigurationchanged

1
推荐指数
1
解决办法
1073
查看次数

当用户在我的应用程序中更改语言时,为什么 android:label 的文本不会更改?

因此,在我的应用程序中,用户看到的第一个活动是选择语言。

假设用户选择法语,然后转到 ActivityB,然后转到 ActivityC。

现在决定更改语言。

因此,返回 ActivityB,然后返回第一个 Activity,并选择语言为西班牙语。

现在,当用户再次转到 ActivityB 时,片段/活动中的所有其他文本都会更改为西班牙语,但 android:label 仍保留法语。如何解决这个问题?

这就是我的 ActivityA 的样子

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             final Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_choose_language, container, false);
        radioGroup = (RadioGroup) rootView.findViewById(R.id.lang_choice_radio);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch(checkedId){
                    case R.id.english_lang:
                        // do operations specific to this selection
                        setLocale("en_US");
                        Intent intentEng = new Intent(getActivity(), Choose_Country.class);
                        startActivity(intentEng);
                        break;

                    case R.id.indonesian_lang:
                        // do operations specific to this selection
                        setLocale("in");
                        Intent intent = new Intent(getActivity(), …
Run Code Online (Sandbox Code Playgroud)

android locale sharedpreferences onconfigurationchanged

1
推荐指数
1
解决办法
3120
查看次数