3

私はそのようなレイアウトを持っています:

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

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@+id/my_image"
    android:ellipsize="end"
    android:singleLine="true"
    android:text="Some text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<ImageView
    android:id="@+id/my_image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/title"
    android:layout_alignBottom="@+id/title"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:adjustViewBounds="true"
    android:src="@drawable/my_bitmap_image" />

このレイアウトは、私が必要とするほとんどのことを行います。画像ビューの高さをテキスト ビューと同じにします。縦横比を保ったまま画像グラフィックコンテンツを引き伸ばします。

しかし、画像ビューの幅は変わりませ!その結果、テキスト ビューと画像ビューの間に大きなギャップができてしまいました。一時的な解決策として、オーバーライドしますView#onLayout

質問: xml レイアウトで画像の幅を変更する方法は?

アップデート:

これは私が必要とする最終的なレイアウトです (テキスト + いくつかの画像)。最初の画像を見てください。その幅は、パディングとマージンなしでスケーリングされた画像とまったく同じでなければなりません:

ここに画像の説明を入力

4

7 に答える 7

4

の場合imageView、画像を線形レイアウトに追加して、重みプロパティを指定できます。たとえば、3 つの画像がある場合、linearlayout重みを次のように指定し3、次に各画像の重みを 1 として指定します。このようにして、すべての画像が同じ幅で均一に配置されます。水平方向の希望として線形方向を作成するので、私の主張は理解できました。

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:weightSum="3"
     >
   <ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:layout_weight ="1" />

    <ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:layout_weight ="1" />

    <ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:layout_weight ="1" />
     </LinearLayout>
于 2012-10-16T09:54:22.443 に答える
2

OK、回答からわかるように、画像ビューの幅と高さを比例して変更することを強制する解決策はありません。したがって、これはプログラムでのみ解決できます。以下に私の解決策があります:

a) カスタム レイアウト クラスを作成する

(すべての親コンストラクターをpublicアクセス修飾子でオーバーライドすることを忘れないでください。そうしないと、GUI エディターが失敗します):

public class MyLayout extends RelativeLayout {...

b) メソッドのオーバーライド: 非推奨 - このメソッドでレイアウト パラメータを変更すると、副作用が発生する可能性があります。以下の推奨されるアプローチを参照してください

protected void onLayout(boolean changed, int l, int t, int r, int b) {

ImageView icon = (ImageView) findViewById(R.id.my_image); icon.setMaxWidth(icon.getMeasuredHeight());

super.onLayout(changed, l, t, r, b);

}

b) 作成方法を追加します。

public static MyLayout createLayout(ViewGroup parent) {
Context context = parent.getContext();
MyLayout item = (MyLayout) LayoutInflater.from(context).inflate(
            R.layout.my_layout, parent, false);    
TextView tv = (TextView) findViewById(R.id.title);
ImageView icon = (ImageView) findViewById(R.id.my_image);
ViewGroup.LayoutParams p = icon.getLayoutParams();
p.width =   (int) tv.getTextSize();;
icon.setLayoutParams(p);
return item;
}

c) 最終レイアウト:

<?xml version="1.0" encoding="utf-8"?>
<com.mypackage.MyLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/my_image"
android:ellipsize="end"
android:singleLine="true"
android:text="Some text"
android:textAppearance="?android:attr/textAppearanceMedium" />

<ImageView
android:id="@+id/my_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/title"
android:layout_alignBottom="@+id/title"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:src="@drawable/my_bitmap_image" />
</com.mypackage.MyLayout>
于 2012-10-16T10:58:00.977 に答える
1

RelativeLayoutの代わりに、LinearLayoutを使用します。必要な間隔を確保するために、「Weight」プロパティを追加できます。

以下のコードを参照してください

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/logonFormButtons"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="true"       
    android:orientation="horizontal">

    <TextView
        android:id="@+id/logonFormBTLogon"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"            
        android:text="Some Text"
        android:layout_weight="0.5" />

    <ImageView
        android:id="@+id/logonFormBTCancel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"            
        android:src="@drawable/apk"
        android:layout_weight="0.5" />
    </LinearLayout>
于 2012-10-16T09:43:04.963 に答える
0

イメージ ビューを Linear Layout に配置します。すべての画像ビューに重みを付けます..それに比例してサイズが大きくなります..

于 2012-10-16T09:27:19.463 に答える
0

私としては、コードで getLayoutParams() を使用して動的に幅または高さを変更することに感謝しています...たとえば

DisplayMetrics displaymetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int height = displaymetrics.heightPixels;
    int wwidth = displaymetrics.widthPixels;
    convertPixelsToDp(wwidth, getApplicationContext());
//    Toast.makeText(getApplicationContext(), height+ "= "+convertPixelsToDp(height, getApplicationContext()) + 
//          "   " + wwidth+ "= "+convertPixelsToDp(wwidth, getApplicationContext()), Toast.LENGTH_LONG).show();
   //scroll view 1 screen height size
    scr1 = (ScrollView) findViewById(R.id.ScrollView01);
    scr1.getLayoutParams().height=height - 200; 
于 2012-10-16T08:59:09.720 に答える
0

scaleType = fitXYORcenterInsideプロパティを imageViewに追加してみてください

于 2012-10-16T08:46:22.730 に答える
0

これはandroid:layout_alignParentRight="true"あなたのImageViewに関係していると思います。試しましたlayout_toRightOf="@id/title"か?

android:scaleType="fitStart"また、ImageViewを試すこともできます。これにより、画像が ImageView の左上に配置されます。

于 2012-10-16T08:44:51.480 に答える