Rotating Views (but not Layouts) on screen orienta

2019-02-26 18:18发布

I would like to rotate Buttons/TextViews/etc. on screen orientation change but I want to keep layout unchanged. How can it be done? I'm using LinearLayouts.

This is what I mean: http://i46.tinypic.com/209hwnr.png

3条回答
Summer. ? 凉城
2楼-- · 2019-02-26 19:01

You'll have to change the layout parameters.

查看更多
成全新的幸福
3楼-- · 2019-02-26 19:12

Create res->layout->layout-land and put your xml file for landscape mode

Your layout file in layout folder is only for portrait mode Now if you need landscape then create layout-land folder.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-02-26 19:20

It can be done it two ways:

1.) Either you define a new xml file in layout-land folder.

2.) Use android:configChanges="orientation" in your activity tag inside manifest.xml

Then in your activity class:

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

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        ChangeToLandscape();
    } else {
        ChangeToPortrait();
    }
}

LayoutParams lp;
public void ChangeToLandscape() {
    lp = new LayoutParams(new ViewGroup.MarginLayoutParams(
            LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
    textView1.setLayoutParams(lp);
    lp.setMargins(0, 0, 0, 0); // Whatever you want
    // Similar for other views
}

//Similarly, implement `ChangeToPortrait()`

Hope it helps !!!

查看更多
登录 后发表回答