2012-01-19 7 views
8

레이아웃과 중력 속성을 사용하여 다른 해상도의 뷰를 중앙에 배치하려고하지만 작동하지 않습니다. 다른 해상도에서 가로로 사용자 지정보기 센터를 만드는 방법이 있습니까?Android에서 맞춤보기를 중앙에 배치하는 방법은 무엇입니까?

onMeasure 메소드에서 어떤 작업을 수행해야합니까? 여기

내가 지금 노력하고있는 것들 중 하나입니다,보기는 가정

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 


    <RelativeLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="50dp" 
     android:gravity="center_horizontal"> 

     <com.application.app.ui.CustomView 
      android:id="@+id/ivCoinAnimation" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true"/> 


    </RelativeLayout> 


</FrameLayout> 
+0

시도한 것을 보여줄 수 있습니까? 중첩 된 LinearLayouts 및 중력 속성을 사용하여 뷰를 가운데에 맞출 수 있어야합니다. – henrik

+0

내가 시도한 것들 중 몇 가지 예를 추가했습니다. – tresnotas

답변

1

, 당신은 'View을 설정합니다 FrameLayout 같은 레이아웃 안에 View이 .. 중앙의 왼쪽에 s layout_gravity to center. gravity과 섞어서는 안됩니다.

또한 실제로 onMeasure()를 재정의해야 할 수도 있습니다. it's documentation을 읽고이를 무시해야하는지 결정하십시오. 그렇다면 매개 변수가 View.MeasureSpec으로 인코딩된다는 점에 유의하십시오.

-1

다음은 화면에 두 개의 버튼을 중앙에 배치하는 방법의 예입니다. 이것은 CustomView에서도 작동합니다.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical"> 

    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal"> 

     <Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
     <Button android:text="Button" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 

    </LinearLayout> 
</LinearLayout> 
0

당신은 사용자 정의 뷰 클래스 그것에서

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 


당신이 당신의 사용자 정의보기에 대한 높이와 폭을 계산한다에 구현해야합니다. 당신은 부모 레이아웃의 중앙 사용자 정의보기에 필요한 경우 는 -을 확인하십시오보기 높이 =

  • 정의 부모보기 높이

및/또는

  • 사용자 정의보기 폭! ! = 상위보기 폭


예를 들어210 :

이제
@Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 

     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     final int height = mCircleRadius * 2; //calculated 
     final int width = getMeasuredWidth(); //parent width 

     setMeasuredDimension(width, height); 
    } 


당신이 당신의 사용자 정의보기 위해 다음 사용할 수 있습니다

android:layout_centerHorizontal="true" 
android:layout_centerVertical="true" 

enter image description here

P.S. android:layout_centerHorizontal="true"은 효과가 없음을 알 수 있습니다. 그 이유는 정확히 너비를 계산하는 대신 부모 폭에있는 측정에 사용하기 때문입니다.

관련 문제