2017-05-19 51 views
1

我已经为纵向和横向模式创建了两种布局,我的场景是我想根据方向更改更改TextView的文本。这里是我的代码: -setContentview()更改onConfigurationChanged()中的布局但未设置android中的数据

的AndroidManifest.xml:

<activity android:name=".MainActivity" 
     android:configChanges="orientation|screenSize|keyboardHidden"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

activity_main.xml中(纵向)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.demo.textdemo.MainActivity"> 
<TextView 
    android:id="@+id/textViewHello" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Hello World!" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:textSize="20dp" /> 

activity_main.xml中(横向):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.demo.textdemo.MainActivity"> 

<TextView 
    android:id="@+id/textViewHello" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:text="Hello World!" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:textSize="20dp" 
    /> 

MainActivity:“Hello World”的

public class MainActivity extends AppCompatActivity { 

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

    textViewHello = (TextView)findViewById(R.id.textViewHello); 
    Configuration newConfig = getResources().getConfiguration(); 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     textViewHello.setText("ORIENTATION LANDSCAPE"); 
    }else { 

     textViewHello.setText("ORIENTATION PORTRAIT"); 
    } 

} 

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); 
     setContentView(R.layout.activity_main); 

     textViewHello.setText("ORIENTATION LANDSCAPE"); 

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ 
     Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); 
     setContentView(R.layout.activity_main); 
     textViewHello.setText("ORIENTATION PORTRAIT"); 
    } 
} 

}

首次,它显示了根据取向的TextView的变化,但是当我改变方向就说明,它定义在activity_main.xml中,我不知道为什么?

回答

0

尝试这样

public class MainActivity extends AppCompatActivity { 

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

textViewHello = (TextView)findViewById(R.id.textViewHello); 
textViewHello.setText("ORIENTATION PORTRAIT"); 

} 
} 


@Override 
public void onConfigurationChanged(Configuration newConfig) { 
super.onConfigurationChanged(newConfig); 
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
    Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); 
    textViewHello.setText("ORIENTATION LANDSCAPE"); 

} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ 
    Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); 
    textViewHello.setText("ORIENTATION PORTRAIT"); 
} 
} 
+0

我已经试过这个,但这不会改变设计它只设置文本,对于横向将背景设置为红色,对于纵向黄色您会看到它的差异,但在方向更改时它不会更改布局。 –

+0

用于更改背景,您需要在活动中以编程方式编写。 – Dhanumjay

+0

我想根据方向更改和设计更改TextView值(需要更改设计背景(仅供参考) –

0

我找到了解决办法。 只是删除从清单文件然后一切这一行工作的罚款

机器人:configChanges =“方向|屏幕尺寸| keyboardHidden”

+0

但在这种情况下,每次方向更改时,Activity都会被销毁并重新创建,我不想要。 –

1

我已经找到了解决方案。当方向发生变化时,我需要将XML对象转换为Java对象,因为如果方向更改布局也会更改,那么它需要将XML转换为Java对象。

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); 
     setContentView(R.layout.activity_main); 
     textViewHello = (TextView)findViewById(R.id.textViewHello); 
     textViewHello.setText("ORIENTATION LANDSCAPE"); 

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ 
     Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); 
     setContentView(R.layout.activity_main); 
     textViewHello = (TextView)findViewById(R.id.textViewHello); 
     textViewHello.setText("ORIENTATION PORTRAIT"); 
    } 

感谢@Dhanumjay

0

不要更改清单文件,并尝试以下code.Now其做工精细而无需重新创建活动

@Override 
public void onConfigurationChanged(Configuration newConfig) { 
    super.onConfigurationChanged(newConfig); 
    setContentView(R.layout.test); 
    textViewHello = (TextView) findViewById(R.id.textViewHello); 
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) { 
     Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show(); 

     textViewHello.setText("ORIENTATION LANDSCAPE"); 

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){ 
     Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show(); 
     textViewHello.setText("ORIENTATION PORTRAIT"); 

    } 
} 
0

在Android中,每当你想设定关于配置数据改变你必须调用两个覆盖方法如下:

@Override 
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { 
    super.onSaveInstanceState(outState, outPersistentState); 
} 

@Override 
public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState) { 
    super.onRestoreInstanceState(savedInstanceState, persistentState); 
} 
+0

是的,我们使用这些方法,但在这种情况下,活动是我不想要的Destroy,感谢您的回答。 –