고정 비율의 레이아웃을 화면에
-
12-12-2019 - |
문제
내 응용 프로그램에,나는 2 개의 선형 레이아웃:상단에 하나,하나의 아래에 있습니다.
내가 좋아하는 무엇이든은 이러한 내부 레이아웃, 의 레이아웃 상단의 60%를 점유 화면 높이 및 레이아웃의 아래쪽 40%.여기에 나 XML 코드:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1.0" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.6"
android:orientation="vertical"
android:id="@+id/layout_top">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="0.4"
android:id="@+id/layout_bottom">
</LinearLayout>
</LinearLayout>
이러한 레이아웃은 문제 없습니다,그들은 그 좋은 비율이다.
문제는 너무 작은 listview 에서 최고의 레이아웃 등다음의 레이아웃을 취할 것입니다 크기의 listview 지 않습을 보존합 60/40%비율이다.
내가 좋아하는 경우에도 내 listview 는 작은(단 3 개 항목에 대한 예를 들어),레이아웃을 보존하의 60%고 그래서 일부를 넣고 빈 공간에 내 listview.
내가 하려고 했 변경 android:layout_height
하기 match_parent
하지만 그것은 아무것도 변경 되지 않습니다.
해결책
이 레이아웃을 사용해보십시오.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1.0" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="6"
android:orientation="vertical"
android:id="@+id/layout_top"
android:background="#FF0000">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="vertical"
android:layout_weight="4"
android:id="@+id/layout_bottom"
android:background="#FF00FF">
</LinearLayout>
</LinearLayout>
.
트릭은 Portrait Mode에서 Wrap_Content 대신 layout_height="0dip"
를 설정하는 것입니다. 가로 모드로 Wrap_Content 대신 layout_with="0dip"
대신 Layout-Land 폴더를 사용할 수 있습니다.
다른 팁
layout_weight LayoutFot보기에서 추가 공간을 뷰를 지정합니다.먼저 화면을 측정해야합니다.
Display display = ((WindowManager)
getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getHeight();
.
와 그런 다음 폭 * 0.6 및 0.4와 같은 일부 계산을 수행하면 레이아웃에 항상 60 개의 40 비율이 있습니다.
희망이 도움이
도 layout_height = 0dp
모두 어린이 LinearLayouts
.그 일이 있기 때문에 당신 wrap_content
아마 재정의 layout_weight
효과가 있다.
단순히 부모의 레이아웃을 대체 android:layout_weight="1.0"
가 android:weightSum="1.0"
이 설정하는 방식으로 작동합중량의 합계 상위 레이아웃과의 무게는 어린이들에 레이아웃이 있습니다.의 무게는 어린이들이 동일해야를 중량의 합이 부모입니다.이 http://blog.stylingandroid.com/archives/312