不同的部件(视图)针对不同的屏幕方向? [英] Different widgets (views) for different screen orientations?
问题描述
我很新开发Android所以我有一个非常基本的问题。
基本上,我有两个屏幕布局,一个用于人像,另一个用于横向。我使用的文件夹RES /布局和RES /布局的土地。两个方向绘制很好,但我对每个方向不同的小部件(按钮),btnPortrait和btnLandscape。
当我尝试调用onSetClickListener()的问题出现了。因为当器件处于画像定向框架无法找到btnLandscape,反之亦然,我即可以使用手动onConfigurationChange()的方向变化。它似乎没有任何工作。
我的code:
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); btnPortrait =(按钮)findViewById(R.id.portraitButton);
tvPortrait =(的TextView)findViewById(R.id.portraitText);
btnLandscape =(按钮)findViewById(R.id.landscapeButton);
tvLandscape =(的TextView)findViewById(R.id.landscapeText);
}
而onConfigurationChange():
公共无效onConfigurationChanged(配置NEWCONFIG){
super.onConfigurationChanged(NEWCONFIG); 如果(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
btnPortrait.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
Toast.makeText(的getContext(),纵向,Toast.LENGTH_SHORT).show();
}
});
} 其他{
btnLandscape.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(视图v){
Toast.makeText(的getContext(),风景,Toast.LENGTH_SHORT).show();
}
});
}
}
祝酒词工作无。
是否有人知道可能会发生?
先谢谢了。
不要区别他们的名字;对于相同功能的部件应共享相同的ID无论是在肖像或风景。
如果它是一个只存在或功能不同的风景,只是检查后findViewById(),如果返回空,你就知道你不是在配置与包括按钮的布局<是空的窗口小部件/ p>
I'm quite new developing for Android so I have a very basic question.
Basically, I have two screen layouts, one for portrait and another for landscape orientation. I use the folders res/layout and res/layout-land. Both orientations are drawn fine, but I have different widgets (buttons) for each orientation, btnPortrait and btnLandscape.
The problem arises when I try to call onSetClickListener(). Because when the device is oriented in portrait the framework can't locate the btnLandscape and viceversa, I handle the orientation changes manually using onConfigurationChange(). It doesn't seem to work either.
My code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnPortrait = (Button) findViewById(R.id.portraitButton);
tvPortrait = (TextView) findViewById(R.id.portraitText);
btnLandscape = (Button) findViewById(R.id.landscapeButton);
tvLandscape = (TextView) findViewById(R.id.landscapeText);
}
And the onConfigurationChange():
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
btnPortrait.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "Portrait",Toast.LENGTH_SHORT).show();
}
});
}
else {
btnLandscape.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getContext(), "Landscape", Toast.LENGTH_SHORT).show();
}
});
}
}
None of the Toasts work. Does somebody know what could be happening?
Thanks in advance.
Don't name them differently; the widgets for the same function should share the same ID whether it's in portrait or landscape.
If it's a widget that only exists or functions differently in landscape, just check if it's null after findViewById(), and if that returns null, you know you're not in a configuration with a layout that includes that button.
这篇关于不同的部件(视图)针对不同的屏幕方向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!