调整布局方向在Java设备的方向不是XML [英] Adjusting layout Orientation with the device orientation in JAVA not XML
本文介绍了调整布局方向在Java设备的方向不是XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道我怎么能根据JAVA设备的方向改变我的LinearLayout方向,我发现如何通过XML的方式与布局和布局土地做,但我没有找到如何在java做方式。
I wanted to know how i could change my LinearLayout orientation according to the device Orientation in JAVA, i found how to do it by the XML way with layout and layout-land but i didn't find how doing it by the java way.
非常感谢你。
推荐答案
请参见此内容它描述了如何检测方向改变然后更改在Java的方向
See this it describes how to Detect Orientation Changed then change orientation in java
在的onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearlayout=........;
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// landscape
linearlayout.setOrientation(LinearLayout.HORIZONTAL);
}
else {
// portrait
linearlayout.setOrientation(LinearLayout.VERTICAL);
}
....
}
和 onConfigurationChanged()
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// landscape
linearlayout.setOrientation(LinearLayout.HORIZONTAL);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
// portrait
linearlayout.setOrientation(LinearLayout.VERTICAL);
}
}
这篇关于调整布局方向在Java设备的方向不是XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文