0

Android コードに問題があります。次のコードがあります

<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 でパーセンテージを操作しているときに、それはどのようになるでしょうか?

4

1 に答える 1