android– 如何防止布局的方向更改,而不是整个屏幕活动

android– 如何防止布局的方向更改,而不是整个屏幕活动,第1张

概述我需要一个子布局(可以是任何布局,如FrameLayout或RelativeLayout)来忽略方向更改并始终保持横向方向.但不是它的父母或任何其他兄弟布局/视图,他们应该相应地改变他们的方向.因此,我不能使用setRequestedOrientation()或android:screenOrientation=“landscape”.或者它将锁定整

我需要一个子布局(可以是任何布局,如FrameLayout或relativeLayout)来忽略方向更改并始终保持横向方向.但不是它的父母或任何其他兄弟布局/视图,他们应该相应地改变他们的方向.

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

我的布局XML:

relativeLayout (Parent)  TextVIEw    FrameLayout/relativeLayout <-- this need to be locked      FrameLayout        button

解决方法:

这可能取决于你的意思是“不改变方向”.但是,我认为最好的起点是为不应该改变的部分创建自己的类.所以布局xml现在有两个文件:

main_layout.xml

relativeLayout (Parent)    TextVIEw        MyNonChangingLayout

my_non_changing_layout.xml

 relativeLayout     FrameLayout         button

你创建的地方

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@OverrIDgeonConfigurationChanged(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:@overrIDeprotected 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 – 如何防止布局的方向更改,而不是整个屏幕/活动所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1117769.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存