방향 변경 시 프로그래밍 방식으로 EditText 크기 조정
-
12-12-2019 - |
문제
프로그래밍 방식으로 크기를 조정하려고 합니다. EditText
전화기의 방향이 바뀌면 지금까지 내가 가진 것은 다음과 같습니다.
private final float editTextWidthLandscap = 380;
private final float editTextWidthPortrait = 220;
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Resources r = getResources();
float px;
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editTextWidthLandscap, r.getDisplayMetrics());
input.setWidth((int) px); // input is an EditText
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editTextWidthPortrait, r.getDisplayMetrics());
input.setWidth((int) px); // input is an EditText
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
내 매니페스트 파일에도 다음이 있습니다(중요한 경우).
<activity
android:name=".Results"
android:configChanges="keyboardHidden|orientation"
android:theme="@android:style/Theme.NoTitleBar"
android:windowSoftInputMode="stateHidden" >
</activity>
이상한 점은 다음을 표시한다는 것입니다. Toast
괜찮지만 무시하는 것 같아요 input.setWidth()
성명.누구든지 그 이유를 설명할 수 있다면 EditText
크기가 조정되지 않고/또는 이를 올바르게 수행할 수 있는 방법에 대한 조언을 제공해 주시면 대단히 감사하겠습니다.
편집하다:
이것은 EditText가 있는 XML 파일의 일부입니다.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="@color/purple"
android:gravity="center_horizontal|center_vertical"
android:orientation="horizontal" >
<EditText
android:id="@+id/input"
android:layout_width="220dp"
android:layout_height="50dp"
android:background="@drawable/search_bar"
android:hint="@string/input_hint"
android:inputType="text|textNoSuggestions"
android:maxLines="1"
android:minHeight="50dp"
android:minWidth="220dp"
android:selectAllOnFocus="true"
android:singleLine="true"
android:textColor="@color/black_grey"
android:textSize="16.67sp" />
<Button
android:id="@+id/search_button"
android:layout_width="65dp"
android:layout_height="50dp"
android:background="@drawable/search_button" />
</LinearLayout>
편집 2:(해결책)
난 아직도 뭔지 모르겠어 input.setWidth()
정확하지만 사용해야 한다는 것을 알았습니다. LayoutParams
이 문제에 대해서는.다음은 현재 나를 위해 작동하는 코드입니다.
private final float editTextWidthLandscap = 380;
private final float editTextWidthPortrait = 220;
private final float editTextHeight = 50;
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Resources r = getResources();
float widthpx;
float heightpx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editTextHeight, r.getDisplayMetrics());;
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
widthpx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editTextWidthLandscap, r.getDisplayMetrics());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) Math.round(widthpx), (int) Math.round(heightpx));
trademarkTextfield.setLayoutParams(lp);
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
widthpx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, editTextWidthPortrait, r.getDisplayMetrics());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams((int) Math.round(widthpx), (int) Math.round(heightpx));
trademarkTextfield.setLayoutParams(lp);
}
}
해결책
내 경험에 따르면 때때로 우리는 나쁜 행동의 이유를 지나치게 생각하는 경향이 있습니다.나는 내가 당연하게 여겼던 일들이 그렇지 않다는 것을 여러 번 깨달았습니다.
두 경우의 px 인수가 실제로 다른지 확인해야 합니다.나는 이것이 기본이라는 것을 알고 있지만 아마도 applyDimension
예상대로 작동하지 않거나 다른 곳에 정의된 최소 너비가 있어서 그냥 쳤습니다.
전화도 해봐야지 getWidth()
통화 전과 후 setWidth()
.왜?아마도 기능은 작동 중이고 시스템에서는 너비가 변경되었으며 문제는 다른 곳에 있다고 생각하기 때문입니다.
다른 팁
크기가 프로그래밍 방식으로 변경되지 않는 이유를 정확히 모르겠지만 가로 모드에 대해 다른 레이아웃 파일을 대신 사용할 수 있습니다.'layout-land'라는 새 폴더를 만들고 거기에 (동일한 ID와 파일 이름을 가진)layout.xml 파일을 넣고 그에 따라 크기를 변경하세요.
귀하의 경우에 따라 이것이 최선의 해결책이 아닐 수도 있지만, 또 다른 옵션일 뿐입니다...