我有纵向模式的不同版面设计和景观模式的不同外观布局..内部' res'文件夹,我创建了一个名为' land-layout'的新文件夹,其中我创建了与内部' layout'。
相同名称的文件。我有textview(counter),当方向改变时,它会恢复该值。 但我面临的问题是,当方向从纵向到横向时,设计仍然与纵向模式相同。 我想知道,我该如何处理? 我要感激不尽。感谢。
MainActivity.java:
public class MainActivity extends Activity {
private int sum = 0;
Button add, sub;
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.textView1);
add = (Button) findViewById(R.id.button1);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
sum++;
tv1.setText("Incrementation = " + sum);
}
});
sub = (Button) findViewById(R.id.button2);
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
sum--;
tv1.setText("Decrementation = " + sum);
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
// setContentView(R.layout.activity_main);
Log.v("message", "ORIENTATION_LANDSCAPE");
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.v("message", "ORIENTATION_PORTRAIT");
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putInt("sum", sum);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
int x = savedInstanceState.getInt("sum");
}
}
res \ layout-land \ activity_main.xml(用于横向模式)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/backg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="total is = 0"
android:layout_gravity="center"
android:textSize="40sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/button1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="ADD" />
<Button
android:id="@+id/button2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:text="Sub" />
</LinearLayout>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
res \ layout \ activity_main.xml(对于纵向模式):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/backg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:freezesText="true"
android:text="total is = 0"
android:textSize="40sp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="ADD" />
<Button
android:id="@+id/button2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Sub" />
</LinearLayout>
清单中的我添加了:android:configChanges =&#34; orientation | screenSize&#34; (我如何使用onConfigurationChanged()来处理布局?)
<activity
android:name="com.example.apptest.MainActivity"
android:configChanges="orientation|screenSize"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.apptest.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
答案 0 :(得分:0)
当你写这个
android:configChanges="orientation|screenSize"
清单中的:这意味着您将自己处理方向更改。所以写这样的东西来处理配置变化。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
...
}
请注意:执行此操作:您将从xml中扩充新布局。因此,将创建新的View
,并且先前视图的内容将丢失。
为了获得更好的用户体验,您需要在新视图中传输旧视图中的内容:
这样的事情:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
CharSequence tmp = ((TextView)findViewById(R.myId)).getText();
setContentView(R.layout.activity_main);
((TextView)findViewById(R.myId)).setText(tmp);
...
}
但是 Android提供了有用的钩子,可以更好地编写所有这些内容。
从清单
中删除此行android:configChanges="orientation|screenSize"
并使用:
@Override
protected void onSaveInstanceState(Bundle outState) {
//here store your data in outState
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState!=null){
//at this point savedInstanceState contains everything that was stored in outState.
}