2017-03-27 31 views

回答

0

可以使用的方法從活動被稱爲: getRequestedOrientation()

所以,你可以這樣做:

if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) { 
     dLayout.setBackgroundColor(getResources().getColor(R.color.bpblack)); 
    } else { 
     dLayout.setBackgroundColor(getResources().getColor(R.color.bpBlue)); 
    } 

將這個上YOUT活動的onCreate每一次你改變你的方向,的onCreate將被再次調用,你的背景會改變它的顏色。

祝你好運。

+0

它沒有工作,當我改變我的設備方向的應用程序停止,在這裏我的代碼: '保護無效onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Ll =(LinearLayout)this.findViewById(R.id.layo); txt =(TextView)findViewById(R.id.text); (getRequestedOrientation()== ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){ Ll.setBackgroundColor((Color.GREEN)); } else { Ll.setBackgroundColor((Color.BLACK)); }' –