2013-12-17 4 views
0

응용 프로그램 화면에 내 사용자 정의보기의 위치를 ​​지정하는 데 문제가 있습니다. "android : layout_alignParentLeft."에 지정된대로 그릴 위치를 '알 수 있도록 onDraw 메서드를 재정의하고 싶습니다. 현재'내 탱크 '위치에 관계없이 marginLeft 및 marginTop에 그려집니다. 어떻게 할 수 있습니까? ?onDraw 메서드를 재정 의하여 각각 android에 인쇄합니다. alignParentLeft

<ImageView 
    android:id="@+id/tank" 
    android:layout_width="wrap_content" 
    android:layout_height="250dp" 
    android:layout_marginLeft="150dp" 
    android:layout_marginTop="10dp" 
    android:adjustViewBounds="true" 
    android:scaleType="fitXY" 
    android:src="@drawable/tank" /> 

<pl.edu.agh.scada.graphicalObjects.AnalogBar 
    android:id="@+id/analogBar" 
    android:layout_width="25dp" 
    android:layout_height="200dp" 
    android:layout_toLeftOf="@+id/tank" 
    android:layout_toStartOf="@+id/tank" 
    android:layout_marginLeft="100dp" 
    android:layout_marginTop="15dp" 
    custom:editable="true"/> 

그리고 사용자 정의보기에 대한 정보를 검색 내 클래스의 부분 :이

XML 파일을 수정은 p가 후

int width; 
int height; 
public AnalogBar(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    boolean editable; 
    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AnalogBar, 0, 0); 
    if (!isInEditMode()) 
     parentView = ((MainActivity) context).fragment; 
    try { 
     editable = a.getBoolean(R.styleable.AnalogBar_editable, false); 
     //I deleted everything else regarding custom properties 
    } finally { 
     a.recycle(); 
    } 
    if (editable) 
     this.setOnClickListener(new WriteRegisterListener()); 
    init(); 
} 

public void init() { 
    if (!isInEditMode()) 
     parentView.addViewToList(this); 

는 레이아웃에 대한 정보를 얻으려면 내가 다른 기능을 시도

public void onSizeChanged(int w, int h, int oldw, int oldh) { 
    super.onSizeChanged(w, h, oldw, oldh); 

    width = w; 
    height = h; 

: 나는 오버라이드 화면에 묶여 getX(), getLocationOnScreen() 하지만 여전히 marginLeft 및 marginTop에 저장된 절대 위치에 기록합니다. toLeftOf 및 toStartOf와 관련된 위치에 뷰를 그리는 데 필요한 정보를 얻을 수있는 방법이 있습니까?

다행히도 나는 그것을 할 수있었습니다. 레이아웃의 위치를 ​​어떻게 든 검색해야한다고 생각했습니다. 밝혀 졌을 때, 왼쪽의 '0'위치에서 뷰를 그릴 때 레이아웃의 맨 왼쪽이됩니다.
당신이이 그려 져야 사용자 정의보기를 그릴 수 있습니다 제대로

android:layout_alignBottom="@+id/tank" 
    android:layout_alignLeft="@+id/tank" 
    android:layout_marginBottom="45dp" 
    android:layout_marginLeft="75dp" 

그것을 parametrise 경우 그 방법. 예를 들어, 모든 뷰를 사각형으로 채우려면 onSizeChanged에서와 같이 크기를 가져와야합니다. 그런 다음 canvas.drawRect (0, 0, width, height, border)를 사용하여 사각형을 그립니다.

왜 이러한 매개 변수 (alignBottom & alignLeft)가 toLeftOf 및 toEndOf보다 잘 작동하는지 알 수 없습니다. 이 매개 변수의 차이점은 무엇입니까?

답변

0

보기는 마진 후에 시작되고 (차일하는 내측 마진으로) 고려 패딩 복용 아이 뷰를 그린다.

android:layout_marginLeft="100dp" 

당신이 정말 부모 사이의 공간을 원하는하지 않는 : 당신이 당신의 뷰가 부모의 왼쪽에 정렬 그려 얻을하려는 경우이 경우 는, 당신은 당신의 사용자 정의보기를 레이아웃 안 너의 견해.

다른 것은 당신이 탱크의에 사용자 정의보기의 위치를 ​​설정하는 것입니다 :

android:layout_toLeftOf="@+id/tank" 
android:layout_toStartOf="@+id/tank" 

그래서 당신은 시도 할 수 :

android:layout_alignParentLeft="true" 

는 도움이되기를 바랍니다.

관련 문제