2013-08-14 1 views
0

내 안드로이드 코드에 문제가 있습니다. 다음 코드ImageView를 사용한 RelativeLayout - 요소 위치 계산

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/main_layout_container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#000000" 
android:orientation="vertical"> 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:adjustViewBounds="true" 
    android:layout_centerVertical="true" 
    android:src="@drawable/pdf" /> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/imageView1" 
    android:layout_alignLeft="@+id/imageView1" 
    android:text="Test-Label" 
    android:textColor="@color/red" /> 

</RelativeLayout> 

내가

ImageView iv = (ImageView) findViewById(R.id.imageView1); 

int width = iv.getWidth(); 
int height = iv.getHeight(); 

float percentMarginTop = 58f; 
float percentMarginLeft  = 65f; 
float percentTextSize = 3f; 

float marginTop  = height/100f * percentMarginTop; 
float marginLeft = width/100f * percentMarginLeft; 

TextView tv = (TextView) findViewById(R.id.textView1); 
tv.setTextSize(TypedValue.COMPLEX_UNIT_DIP, Math.round(height/100f * percentTextSize)); 
RelativeLayout.LayoutParams lpTv = (RelativeLayout.LayoutParams) tv.getLayoutParams(); 
lpTv.topMargin = Math.round(marginTop); 
lpTv.leftMargin = Math.round(marginLeft); 

그래서 내가 실제 폭의 계산 된 백분율 값을 가진 요소를 배치 할 다음 코드 내 이미지 뷰에서 이제 텍스트 뷰를 놓고 사용하려는에게 있습니다 및 이미지 뷰의 높이. 그래서 내 생각에 요소는 각 장치의 밀도 (ldpi, mdpi, xhdpi, ...) 및 화면 크기 (작음, 보통 크기, 큰 크기, 크고 크다는)가 아니라 올바른 위치에 있어야합니다. (4 ")은 정확하지만 타블렛 (10")과 같은 다른 장치에서는 올바르지 않습니다. 주어진 ImageView에서 백분율로 작업 할 때 어떻게 될 수 있습니까?)

폭/100 반환하지 플로트 값,하지만 int 값을, 그것이 내가보고 싶지 않은 바보 같은 초보자의 실수 또는 그것을 무시하고 싶었처럼

답변

0

보인다. 그리고 곱셈을하는 잘못된 값 때문에 전체 계산이 실패합니다.

내 코드가 변경되었습니다. 이제는 작동중인 스 니펫