3

注: これをバグとして Android プロジェクトに報告しました: http://code.google.com/p/android/issues/detail?id=39159また、承認された報奨金の回答、残念ながら、解決策は、絶対的な (つまり、「wrap_content」ではなく「dp」などを指定する) レイアウトを使用して問題を解決することです。

画像に背景を配置すると、非常に奇妙な動作が発生します。問題を説明するために、これをかなり単純化しました。私がしているのは、画像を相対レイアウトに配置し、背景も使用することです。relativelayout にパディングを指定すると、画像の背景が誤って描画されるようです。Wrap_content がおかしくなっているようです。

まず、問題を示すコードを次に示します。linearlayout を使用せずにイメージビューに背景を与えるだけで同じ動作が見られることに注意してください。ただし、これは問題をよりよく示しています。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:padding="5dp" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/black_bg" >
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/red_rectangle" />
        </LinearLayout>
    </RelativeLayout>
</FrameLayout>

black_bg xml ファイルは次のとおりです。

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FF000000"/>
</shape> 

red_rectangle は次のとおりです。 red_rectangle

これは、問題を示すための参照イメージであることに注意してください。私の実際の画像には詳細があるため、.9.png にすることはできません

そして、ここに問題のスクリーンショットがあります:

レンダリング エラー

linearlayout の幅が「wrap_content」に設定されているにもかかわらず、画像の幅が linearlayout よりも小さいことがわかります。relativelayout パディングを 0dp に設定すると、この問題はなくなります。

これは私がここで提供しているリソースのかなり充実したセットであることを願っています。

参考までに、これを使用して画像の周囲に境界線を提供しているため、linearlayout (または画像自体) にパディングを設定できますが、その場合でも問題は解決しません。

編集:この境界線を提供する方法に焦点を当てているため、おそらくこれについてもう少しコンテキストが必要なようです。これは、よりコンテキストに応じたレイアウトのスクリーンショットです。問題をさらに混乱させるため、最初にこれを含めたくありませんでした。

スクリーンショット

最初に表示される 5 dp のパディングは、このアイテム全体のコンテンツ (relativelayout) 用です。次に、最初に言ったように、relativelayout に表示される最初のパディングに加えて、「linearlayout (または画像自体) にパディングを設定することができる」という考えです。現在、このレイアウトには境界線が表示されていません。

4

5 に答える 5

4

問題は、画像(画像ビューの場合)と背景として設定されたもの(線形レイアウトの場合)のストレッチプロパティが異なることにあるようです。背景として設定された画像は必ずしもアスペクト比を維持するわけではありませんが、画像内の画像はアスペクト比を維持する傾向があります。レイアウトの高さを60dpにすると、画像はアスペクト比を維持したまま縮小し、側面に黒い帯が残ります。これは私のために働きます:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="60dp"
        android:padding="5dp" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/black_bg" >
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="60dp"
                android:src="@drawable/asd"
                android:scaleType="fitXY"
                 />
        </LinearLayout>
    </RelativeLayout>
</FrameLayout>
于 2012-10-31T09:05:35.513 に答える
1

なぜそこに余分な黒いパッチが表示されているのかわかりません。アプリを実行してみましたか?UIエディターには、特にImageView..に関していくつかの欠陥があります。

次に、画像の周囲の境界線について、背景とパディングをImageViewそれ自体に設定します。LinearLayoutは必要ありません。スケールタイプ属性に"centerInside"値を追加します。

 <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="5dp"
      android:layout_centerInParent="true"
      android:adjustViewBounds="true" 
      android:src="@drawable/red_rectangle"
      android:background="#000" 
      android:scaleType="centerInside"/> 
于 2012-10-26T17:23:42.493 に答える
1

これはバグの良い候補だと思います!

とにかく、このレイアウトで何を達成しようとしているのかは理解できます。問題は、RelativeLayout の高さを設定することです。中身のラッピングはお願いしません!簡単に言えば、高さが 60dp に設定され、パディングが 5dp に設定されているため、さらに一歩進んで、LinearLayout の高さを 50dp に設定します60-2*5:)

最後に、境界線を取得するには、たとえば 5dp のパディングを LinearLayout に追加し、ImageView の Height を に設定します50dp - 2*5 = 40dp

これは完全に機能します


<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:padding="5dp" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:padding="5dp"
            android:background="@drawable/black_bg" >
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="40dp"
                android:adjustViewBounds="true"
                android:src="@drawable/red_rectangle" />
        </LinearLayout>
    </RelativeLayout>
</FrameLayout>

于 2012-10-29T15:24:22.803 に答える
0

これを試して ...........

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:padding="5dp" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/black_bg"
            **android:padding="5dp"**
            >
            <ImageView
                **android:layout_width="fill_parent"
                android:layout_height="fill_parent"**
                android:adjustViewBounds="true"
                android:src="@drawable/red_rectangle" />
        </LinearLayout>
    </RelativeLayout>
</FrameLayout>

このように画像を表示します。

<ImageView 
            android:id="@+id/imageViewMyicon"
            android:layout_width="30dp"
            android:background="@drawable/myiconbackground"
            android:layout_height="30dp"
            android:src="@drawable/myicon"
            android:contentDescription="@string/my_icon"
            />

ドローアブルmyiconbackground.xml内

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
       android:shape="rectangle" 
       android:padding="10dp">
    <solid android:color="#0D95BD"/>
        <corners android:radius="5dp"/>
        <padding android:left="2dp" android:right="2dp" android:top="2dp" android:bottom="2dp"/>
</shape>

私はこれが私のために働いていることを確認しました、あなたのためにもするべきです

于 2012-10-30T10:44:57.257 に答える
0

「参考までに、これを使用して画像の周囲に境界を設けています」

ドローアブル「border.xml」を追加

<shape xmlns:android:"http://schemas.android.com/apk/res/android" android:shape:"rectangle">
 <stroke android:width="5dp" android:color="#FF000000" />
 <padding android:left="5dp" android:top="5dp" android:right="5dp" android:bottom="5dp" />
</shape>

ImageView の背景をこのドローアブルに設定します。

レイアウトを簡素化し、ImageView を中央に配置します。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
        <ImageView 
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:layout_centerInParent="true"
                android:adjustViewBounds="true" 
                android:src="@drawable/red_rectangle"
                android:background="@drawable/border" /> 
</RelativeLayout> 
于 2012-09-12T16:59:05.900 に答える