Layout change drastically on orientation change

前端 未结 4 1028
耶瑟儿~
耶瑟儿~ 2020-12-22 02:43

I have created a layout for an activity. The layout contains some image buttons and text views. The problem is that when I am setting the orientation of my device to portrai

相关标签:
4条回答
  • 2020-12-22 03:19

    first you have to create following folder for different layout.....

    Folder Name                       
    
    layout-ldpi                           
    layout-land-ldpi                    
    
    layout-mdpi                           
    layout-land-mdpi                      
    
    layout-hdpi                           
    layout-land-hdpi                      
    
    layout-xlarge                        
    layout-xlarge-land                    
    

    1...Change in AndroidManifest.xml

    android:configChanges="orientation|keyboardHidden"
    

    2....Create layout_land.xml in layout Folder.

    3....Create layout_port.xml in layout-land Folder.

    4...Create Following Method

    @Override
    public void onConfigurationChanged(Configuration newConfig) 
    {
        super.onConfigurationChanged(newConfig);
    
    
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
        {
            setContentView(R.layout.layout_land);
            set_all_id();
        } 
        else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
        {
            setContentView(R.layout.layout_port);
            set_all_id();
        }
    }
    

    5....Change in OnCreate Method (only this content)

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    
        super.onCreate(savedInstanceState);
        if (getResources().getConfiguration().orientation == 1)
        {
            setContentView(R.layout.layout_port);
        }
        if (getResources().getConfiguration().orientation == 2)
        {
            setContentView(R.layout.layout_land);
        }
        set_all_id();
    
    }
    

    6...Create set_all_id() method for initialization of view objects

    public void set_all_id()
    {
        //define id for all view objects..
        //textview = (textview)findViewById(R.id.textview);
    }
    
    0 讨论(0)
  • 2020-12-22 03:26
    • create folder /res/layout-land (here you will keep your landscape adjusted layouts)
    • copy chooseActivity.xml there
    • make necessary changes to it
    0 讨论(0)
  • 2020-12-22 03:29

    - It seems you screen size is creating a problem here.

    - You will have to create another folder under res directory as layout-land, and in it create a .xml file just like you did in layout folder, and make it look the way you want it in landscape mode.

    - When your app goes into landscape mode, automatically this .xml file will be selected (ie from the layout-land) folder.

    0 讨论(0)
  • 2020-12-22 03:33

    Due to less height in the landscape mode, the layout will display like this.

    According to me the good way is maintain the layout-land in the res folder and design the layout for the landscape mode. so that it will be nice look

    0 讨论(0)
提交回复
热议问题