2

XML を使用した Android UI 設計では、ImageButton をアクティビティ xml ファイルの背景に正確に配置する方法を教えてください。

2 つの画像があり、1 つはアクティビティの背景画像として機能し、2 つ目は画像ボタン ソースとして機能するとします。

これが背景画像です。 http://i.stack.imgur.com/e1Ow5.png

ボタン画像です。http://i.stack.imgur.com/m0tUU.png

最初の画像をアクティビティの背景として設定します。私の質問は、2番目の画像を適切に配置して整列させる方法です。つまり、ボタンが背景の「中央の長方形」の中に正確に収まるようにします。「中央の長方形」はプレースホルダーで、画面のどこにでも配置できます。

注意: 相対レイアウトを使用してみましたが、背景によってはボタンを実際に配置できませんでした。

編集:- 実際には、背景の中央にある長方形と角丸長方形は単なるプレースホルダーです。それは何でもありえます。または、どこでもかまいません。中心ではないかもしれません。画像全体を考慮して、プレースホルダーがある場所にボタン画像を配置する必要があります。それが私の意図です。たとえば、ボリューム ロッカーとして機能する回転ボタンがあるラジオ アプリケーションを考えてみましょう。画像内のそれ以外はすべて背景であり、ボリューム ロッカーは別の画像である可能性があります。

4

3 に答える 3

0

答えは、あなたがやろうとしている方法では不可能です。

あなたがしたいことは、ボタンが中央にくるように背景をさらにカットすることです.

画像を例として使用すると、ビュー階層は次のようになります

->FrameLayout1
---->Framelayout2
-------->Button

FrameLayout1 は、内側の正方形のない背景になります

FrameLayout2 は内側の正方形になります

ボタンはボタン アセットになり、FrameLayout 2 の中央に配置されます。

これに加えて、9 つのパッチ ドローアブルを使用するなど、ピクセル パーフェクトに見せるために必要なテクニックが他にもあります。

于 2014-09-24T04:53:46.283 に答える
0
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/img_bg" />

    </RelativeLayout>

drawable/img_bg.xml
    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

        <item android:drawable="@drawable/bg">
        </item>
        <item android:drawable="@drawable/fr">
        </item>

    </layer-list>

or

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

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/bg" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignTop="@+id/imageView1"
            android:src="@drawable/fr" />

    </RelativeLayout>

bg.png と fr.png は、高さと幅が同じ透明画像です。 bg.png fr.png 最終写真

于 2014-09-24T12:51:46.920 に答える