本文介绍了如何prevent方向的改变只是一个布局,而不是整个屏幕/活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个子布局(可以是任何布局,如的FrameLayout RelativeLayout的)忽略方向改变和横向始终保持。但不是其父母或其他兄弟姐妹的布局/意见,就应该相应地改变其方向。

因此​​,我不能使用 setRequestedOrientation()的android:screenOrientation =风景。或将锁定方向对整个屏幕或活性。我需要锁定才刚刚这个单一的布局。

我的布局XML:

  RelativeLayout的(家长)
  TextView中
    的FrameLayout / RelativeLayout的&下; - 这需要被锁定
      的FrameLayout
        按键


解决方案

这可能取决于你所说的不改变方向的意思。不过,我觉得开始是创建自己的类不应该改变的一部分最好的地方。因此,布局XML现在有两个文件:

main_layout.xml

  RelativeLayout的(家长)
    TextView中
        MyNonChangingLayout

my_non_changing_layout.xml

 的RelativeLayout
     的FrameLayout
         按键

您已经创建

  MyNonChangingLayout延伸的FrameLayout {
    MyNonchangingLayout(上下文内容){
        超级(上下文);
        myContext =背景;
        makeFromXML();
    }私人无效makeFromXML(){
    LayoutInflater吹气=(LayoutInflater)myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    冠捷= inflater.inflate(MyR.layout.my_non_changing_layout,这一点,假);    //获取这里的所有子视图中使用topView.findViewById()    //执行查看其他任何启动你需要在这里    //确保你这个否则它不会实际出现!
    super.addView(冠捷);
}/ *
 *然后,您可以覆盖相当多的布局的电话和
 *加强对孩子的行为。两个例子:
 * ///为了专门捕获方向变化
@Overridge
onConfigurationChanged(配置NEWCONFIG){
    super.onConfigurationChanged(NEWCONFIG);
    //你可以通过删除所有意见,并重建他们在这里创建布局
    //也许由具有两个XML布局,其中一个是90度..​​.
    //如果你做layot​​这里,请确保您不构造code交锋!
    开关(newConfig.orientation){
        案例ORIENTATION_LANDSCAPE:
            //使这个方向布局(根据上文)
            打破;
        案例ORIENTATION_PORTRAIT:
            //使这个方向布局(根据上文)
            打破;
        案例ORIENTATION_SQUARE:
            //使这个方向布局(根据上文)
            打破;
    }
}//处理大小的变化来执行对儿童的长宽比:
@override
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
    super.onSizeChanged(W,H,oldw,oldh);    INT viewWidth = //事情我已经决定
    INT viewHeight = //事情我已经决定
    setViewsize(viewToHaveSizedControlled,viewWidth,viewheight);
}//该职位的事情意味着它不会崩溃不管它是哪个线程
//上执行...
私人无效setViewsize(最终视图V,最终诠释W,最终诠释高){
    后(新的Runnable(){
        公共无效的run(){
            ViewGroup.LayoutParams LP = v.getLayoutParams();
            lp.width = W;
            lp.height = H;
            v.setLayoutParams(LP);
    }});
}

}

您可以再执行pretty你想好什么。如果你能具体谈谈要强制上分区域什么样的行为,我可能能够提出更具体的code。

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

I would need a child layout (can be any layout, like FrameLayout or RelativeLayout) to ignore orientation change and remain always in landscape orientation. But not its parent or any other sibling layouts/views, they should change their orientation accordingly.

Hence, I cannot use setRequestedOrientation() or android:screenOrientation="landscape". Or it will lock the orientation for the whole screen or the activity. I need to lock only just this single layout.

My layout XML:

RelativeLayout (Parent)
  TextView
    FrameLayout/RelativeLayout <-- this need to be locked
      FrameLayout
        Button
解决方案

It probably depends what you mean by "not change orientation". However, I think that best place to start would be to create your own class for the part that shouldn't change. So the layout xml now has two files:

main_layout.xml

RelativeLayout (Parent)
    TextView
        MyNonChangingLayout

my_non_changing_layout.xml

 RelativeLayout
     FrameLayout
         Button

Where you have created

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);
    }});
}

}

You can then enforce pretty well anything you want. If you can be more specific about what behaviour you want to enforce on the sub region I might be able to suggest more specific code.

One thing you may be wanting to do is to keep

这篇关于如何prevent方向的改变只是一个布局,而不是整个屏幕/活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:41