int mode = getResources().getConfiguration().orientation; // 1 = Portrait // 2 = Landscape if (mode == 1) { result.setText("Current Portrait"); } else { result.setText("Current Landscape"); }
int mode = getResources().getConfiguration().orientation; if (mode == Configuration.ORIENTATION_PORTRAIT) { result.setText("Current Portrait"); }else if (mode == Configuration.ORIENTATION_LANDSCAPE) { result.setText("Current Landscape"); }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/txtResult" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:layout_marginTop="130dp" android:text="Result" android:gravity="center_horizontal" android:textAppearance="?android:attr/textAppearanceMedium" /> </RelativeLayout>
package com.myapp; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView result = (TextView) findViewById(R.id.txtResult); int mode = getResources().getConfiguration().orientation; if (mode == 1) { result.setText("Current Portrait"); } else { result.setText("Current Landscape"); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท