Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何防止布局的方向更改,而不是整个屏幕/活动大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要一个子布局(可以是任何布局,如FrameLayout或RelativeLayout)来忽略方向更改并始终保持横向方向.但不是它的父母或任何其他兄弟布局/视图,他们应该相应地改变他们的方向.

因此,我不能使用setrequestedOrientation()或android:screenOrientation =“landscape”.或者它将锁定整个屏幕或活动的方向.我只需要锁定这个单一的布局.

我的布局XML

RelativeLayout (Parent)
  TextView
    FrameLayout/RelativeLayout <-- this need to be locked
      FrameLayout
        Button
@H_489_14@@L_616_3@
这可能取决于你的意思是“不改变方向”.但是,我认为最好的起点是为不应该改变的部分创建自己的类.所以布局xml现在有两个文件

@H_649_5@main_layout.xml

RelativeLayout (Parent)
    TextView
        MyNonChangingLayout
@H_649_5@my_non_changing_layout.xml

RelativeLayout
     FrameLayout
         Button

你创建的地方

@H_155_11@myNonChangingLayout extends FrameLayout { MyNonchangingLayout(Context content) { super(context); myContext = context; makeFromXML(); } private void makeFromXML() { LayoutInflater inflater = (LayoutInflater)myContext.getSystemservice(Context.LAYOUT_INFLATER_serviCE); topView = inflater.inflate(MyR.layout.my_non_changing_layout,this,falsE); // Get all the sub Views here using topView.findViewById() // Do any other initiation of the View you need here // Make sure you this otherwise it won't actually appear! super.addView(topView); } /* * Then,you can override quite a lot of the layout's calls and * enforce behavIoUr on the children. Two examples: */ // To specifically catch orientation changes @Overridge onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); // You Could create the layout here by removing all views and rebuilding them // Perhaps by having a two xml layouts,one of which is "90 degrees out" ... // If you do make the layot here,make sure you don't clash with the constructor code! switch (newConfig.orientation) { case ORIENTATION_LANDSCAPE: // Make the layout for this orientation (as per abovE) break; case ORIENTATION_PORTraiT: // Make the layout for this orientation (as per abovE) break; case ORIENTATION_SQUARE: // Make the layout for this orientation (as per abovE) break; } } //to handle size changes to enforce aspect ratios on children: @override protected void onSizeChanged (int w,int h,int oldw,int oldh) { super.onSizeChanged(w,h,oldw,oldh); int viewWidth = //something I've determine int viewHeight = //something I've determined setViewsize(viewToHaveSizedControlled,viewWidth,viewheight); } // The post thing means that it doesn't crash no matter which thread it is // executed on ... private void setViewsize(final View v,final int w,final int h) { post(new Runnable() { public void run() { ViewGroup.LayoutParams lp = v.getLayoutParams(); lp.width = w; lp.height = h; v.setLayoutParams(lp); }}); }

}

然后,您可以很好地执行您想要的任何事情.如果您可以更具体地了解要在子区域上执行的行为,我可能会建议更具体的代码.

你可能想要做的一件事就是保持

大佬总结

以上是大佬教程为你收集整理的android – 如何防止布局的方向更改,而不是整个屏幕/活动全部内容,希望文章能够帮你解决android – 如何防止布局的方向更改,而不是整个屏幕/活动所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。