2013-09-30 2 views
1

이미지보기와 텍스트 상자를 포함하는 몇 가지 모눈 항목이있는 메뉴 화면을 만들려고합니다. 내 의도는 다른 화면 해상도/크기에 대해 동일한 척도를 얻는 것입니다.그리드 항목의 동적 크기 조정

예 : 아래 이미지는 내가 만들고자하는 것입니다. 보시다시피 화면 크기에 따라 그리드 항목의 크기가 변하지 만 여전히 화면에서 동일한 비율을 차지합니다.

Getting screen dimentions

enter image description here

, 다른 화면 크기에 대한 각 격자 항목에 대한 대략적인 크기를 만드는 큰 문제가되지 않습니다. 그러나 적절한 여백을 설정하고 화면의 격자보기 중심을 두는 것은 지루합니다.

더 쉬운 방법이 있습니까?

grid_item.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:layout_margin="5dp" 
android:orientation="vertical" > 

<ImageView 
    android:id="@+id/menu_item_image" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_weight="2" 
    android:contentDescription="@string/menu_item" /> 

<TextView 
    android:id="@+id/menu_item_text" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:layout_weight="8" 
    android:gravity="center" /> 

</LinearLayout> 

activity_main_menu.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/LinearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainMenuActivity" > 

<GridView 
    android:id="@+id/mainGridView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center_vertical|center_horizontal" 
    android:layout_margin="20dp" 
    android:numColumns="2" > 

</GridView> 

</LinearLayout> 

답변

0

가장 쉬운 방법은 다른 dimens.xmlvalues에서 폴더를 만드는 것입니다.

단계 :

  1. 이미지 뷰에 대한 당신이 원하는 뷰의 치수 값을 정의 값과 같은 특정 해상도 값-sw600dp, 값-sw720dp
  2. 값 폴더에 dimens.xml를 만듭니다. (차원 변수 이름은 모든 dimens.xml 파일에서 동일해야합니다.)
  3. 이제 dimens.xml을 참조하십시오. 그러면 화면의 크기와 밀도에 따라 치수 값이 적용됩니다.