この図面のように、ImageViewの特定の場所の上にTextViewを配置する必要があるImageViewがあります。
私はなんとか次のxmlを使用してこれを行うことができました:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/background"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:src="@drawable/imageBg"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test text"
android:textColor="#ff0000"
android:layout_marginRight="200dp"
android:layout_marginTop="205dp"
app:layout_constraintLeft_toLeftOf="@id/background"
app:layout_constraintRight_toRightOf="@id/background"
app:layout_constraintTop_toTopOf="@id/background" />
</androidx.constraintlayout.widget.ConstraintLayout>
これは私の1つのエミュレーターテストデバイスで正常に機能しますが、他のエミュレーターデバイスで試したときに予想したように、固定マージンのdp値のため、異なる解像度などを実行する複数のデバイス間でうまくスケーリングしません。
複数のデバイス間で機能し、TextViewが常に同じ位置にあるようにするには、どうすればこれを最良の方法で実現できますか?私はdimens.xmlを利用し、そこで異なるデバイスディメンションに異なるマージン値を使用できることを知っていますが、これが最善のアプローチであるとは思いません。
私はdimens.xmlを利用し、そこで異なるデバイスディメンションに異なるマージン値を使用できることを知っていますが、これが最善のアプローチであるとは思いません。
正しいです。次のようなガイドラインを使用して、レイアウトが異なる画面で同じように見えるようにすることができます。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.8"
tools:srcCompat="@tools:sample/backgrounds/scenic" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5"/>
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.45"/>
<TextView
android:id="@+id/textView7"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/colorAccent"
app:layout_constraintHeight_percent="0.1"
app:layout_constraintWidth_percent="0.2"
android:text="TextView"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintEnd_toStartOf="@+id/guideline3" />
</androidx.constraintlayout.widget.ConstraintLayout>
これは次のようになります(黒い矢印はガイドラインです):
これでレイアウトは1つだけになりましたが、レスポンシブレイアウトです。さまざまなデバイス用に追加でレイアウトを作成する必要はありません。
私のビューには固定サイズがないことに注意してください。パーセンテージを使用して完全にレスポンシブなレイアウトを作成しました。この件について詳しくは、私の回答を確認してください。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加