2017-05-19 3 views
1

세로 및 가로 모드의 두 가지 레이아웃을 만들었습니다. 시나리오는 방향 변경을 기반으로 TextView의 텍스트를 변경하려고합니다. 여기 내 코드는 다음과 같습니다 -setContentview()는 onConfigurationChanged()에서 레이아웃을 변경하지만 안드로이드에서 데이터를 설정하지 않습니다.

의 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 : "안녕하세요"를

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"); 
    } 
} 

} 처음

, 그것은 텍스트 뷰의 변화는 방향에 따라하지만 난 방향을 변경할 때 보여줍니다 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

처럼하려고합니다. "| 화면 크기 | keyboardHidden 방향을"

+0

그러나이 경우, 오리엔테이션 변경 활동이 파괴되고 내가 원치 않는 활동을 재현 할 때마다. –

1

나는 해결책을 발견 configChanges = 는 잘

안드로이드 작업 매니페스트 다음 파일 모두에서이 줄을 제거합니다. XML 객체를 Java 객체로 변환 할 때 방향 변경이 필요합니다. 방향 변경 레이아웃이 변경되어 XML을 Java 객체로 변환해야하기 때문입니다.

0

@Dhanumjay

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"); 
    } 

덕분에 매니페스트 파일을 변경하고 변경 구성에 대한 데이터를 설정할 때마다 안드로이드에서 활동

@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

를 다시하지 않고는 제대로 작동 code.Now 아래에 시도하지 말라 다음과 같이 두 가지 재정의 방법을 호출해야합니다.

@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

예, 우리는이 방법을 사용합니다. 그러나이 경우 Activity는 내가 원하지 않는 Destroy입니다. 답변에 감사드립니다. –

관련 문제