Layout change drastically on orientation change

前端 未结 4 1065
耶瑟儿~
耶瑟儿~ 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条回答
  •  猫巷女王i
    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);
    }
    

提交回复
热议问题