0

プログラムで水平スクロールビュー内のイメージビューにテキストビューを追加しようとしています。ただし、これは機能しないようです。スクロールなしの RelativeLayout のサンプル画像: スクリーン キャプチャ 1

水平スクロールのサンプル画像を キャプチャー2 次に示します。これが私の xml レイアウトです。

        <HorizontalScrollView android:id="@+id/home_horizontal_scroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/middle_menu_title" >    

            <LinearLayout android:id="@+id/home_linear_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
                </LinearLayout> 
        </HorizontalScrollView>

私のテストコード内:

     LinearLayout layout = (LinearLayout)findViewById(R.id.home_linear_layout);
    for(int i = 0 ; i < 10; i++){
        ImageView myView = new ImageView(this);
        myView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        myView.setAdjustViewBounds(true);
        myView.setScaleType(ScaleType.FIT_XY);
        myView.setPadding(0, 2, 2, 0);
        myView.setImageResource(R.drawable.render);
        layout.addView(myView);


        TextView text = new TextView(this);

        text.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 50));
        text.setBackgroundColor(Color.parseColor("#4000"));
        text.setTextColor(Color.WHITE);
        text.setGravity(Gravity.CENTER);
        text.setText("Header Title");

        layout.addView(text);  

また、水平スクロールビュー内で相対レイアウトを使用してみましたが、成功しませんでした。以下のような単純な相対レイアウト内では、タイトルと画像を表示できますが、水平スクロールビューにある場合は表示できません

 <RelativeLayout android:id="@+id/top_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/menu_title"
        android:background="@drawable/background_gradient">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="2dip"

            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/render" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentBottom="true"
        android:background="#4000"
        android:gravity="center"
        android:text="Image Title"
        android:textColor="#FFFFFF" />
    </RelativeLayout>

何かアドバイスはありますか?

4

2 に答える 2

0

レイアウトに問題があります:

LinearLayout 親ビューに、その子に応じて幅を取るように言います:

使用して android:layout_width="wrap_content"

次に、親に従って幅を取るように子に言います:

使用してLayoutParams.MATCH_PARENT

両者は互いに依存しているため、予測可能な結果が得られないことを理解する必要があります。

子の幅を LayoutParams.WRAP_CONTENT に設定すると、問題が解決すると思います。

    ImageView myView = new ImageView(this);
    myView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
    myView.setAdjustViewBounds(true);
    myView.setScaleType(ScaleType.FIT_XY);
    myView.setPadding(0, 2, 2, 0);
    myView.setImageResource(R.drawable.render);
    layout.addView(myView);


    TextView text = new TextView(this);

    text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 50));
    text.setBackgroundColor(Color.parseColor("#4000"));
    text.setTextColor(Color.WHITE);
    text.setGravity(Gravity.CENTER);
    text.setText("Header Title");

    layout.addView(text);

EDIT : 質問の編集を見て、 LinearLayout は、子のオーバーラップを許可しないため、良い答えにはなりません。

于 2013-03-04T12:29:12.057 に答える
0

compoud drawables を使用すると、新しい親レイアウトに配置することなく、 TextViewに画像を簡単に追加できます。

myTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.left, 0, 0, 0);

ドローアブルを削除するには 0 を入力します。ドローアブルは任意の側 (左、上、右、下) に配置できます。こちらのドキュメントを参照してください。役立つ場合があります。ドローアブルの固有の境界を使用するため、ドローアブルのサイズはニーズに一致する必要があります。

LinearLayoutに画像とテキスト以外のビューがない場合は、最適化のために複合ドローアブルを使用することをお勧めします。

編集:質問の編集を見ました:画像とテキストを重ねる必要がある場合、複合ドローアブルは答えになりません。

于 2013-03-06T08:27:00.220 に答える