Different widgets (views) for different screen orientations?

半城伤御伤魂 提交于 2019-12-13 04:32:54

问题


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.


回答1:


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.




回答2:


Just make sure that your button Id in layout-land is same Id in default layout

Update1 :

ok add not null check before using the widget like this

if( btnPortrait !=null ) { 
// do what you want 
}



回答3:


I'm not sure by what you mean in the comments by "they're not the same button" but a way of doing what you want with two different resource ids would be as follows...

public class MyActivity extends Activity {

    Button myButton = null;
    TextView myTextView = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myButton = (Button) findViewById(R.id.portraitButton);
        if (myButton == null)
            myButton = (Button) findViewById(R.id.landscapeButton);
        myTextView = (TextView) findViewById(R.id.portraitText);
        if (myTextView == null)
            myTextView = (TextView) findViewById(R.id.landscapeText);
    }

    // The following method is your OnClickListener
    public void sayHello(View v) {
        switch (v.getId) {
            case R.id.portraitButton :
                // Do something for portrait
                break;
            case R.id.landscapeButton :
                // Do something for landscape
                break;
        }
    }
}

In your layout files you would then assign the OnClickListener using android:onClick as follows...

For portrait...

<Button
    android:id="@+id/portraitButton"
    ...
    android:onClick="sayHello" >
</Button>

For landscape...

<Button
    android:id="@+id/landscapeButton"
    ...
    android:onClick="sayHello" >
</Button>

In this way, the OnClickListener is set by the inflation process when setContentView(...) is called. It then simply comes down to it determining the resource id to decide what action needs to be taken.



来源:https://stackoverflow.com/questions/17661516/different-widgets-views-for-different-screen-orientations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!