98

画像付きの imageView があり、その画像の上にテキストを配置します。どうすればそれを達成できますか?

4

9 に答える 9

165

それが私がそれをした方法であり、RelativeLayout内であなたが要求したとおりに機能しました:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativelayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

    <TextView
        android:id="@+id/myImageViewText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/myImageView"
        android:layout_alignTop="@id/myImageView"
        android:layout_alignRight="@id/myImageView"
        android:layout_alignBottom="@id/myImageView"
        android:layout_margin="1dp"
        android:gravity="center"
        android:text="Hello"
        android:textColor="#000000" />

</RelativeLayout>
于 2011-11-25T15:23:01.253 に答える
17

別の側からの場合は、次のようにすることをお勧めします。背景にドローアブルを備えたTextViewを使用する方が簡単なようです。

 <TextView
            android:id="@+id/text"
            android:background="@drawable/rounded_rectangle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        </TextView>
于 2012-10-26T14:56:53.163 に答える
6

これを実現するには、FrameLayout または Merge レイアウトを使用します。Android dev guide には、この良い例があります: Android Layout Tricks #3: Optimize by merging

于 2011-03-09T08:14:19.303 に答える
6

あなたはおそらく

  • クラスImageViewから継承された新しいクラスを作成し、
  • Method をオーバーライドしますonDrawsuper.onDraw()最初にそのメソッドを呼び出して、
  • 次に、表示したいテキストを描きます。

そのようにすると、これを単一のレイアウト コンポーネントとして使用できるため、他のコンポーネントと一緒にレイアウトしやすくなります。

于 2011-03-09T08:14:58.557 に答える
5

多くの方法があります。RelativeLayout または AbsoluteLayout を使用します。

相対では、たとえば、画像を左側の親と揃え、テキストも親の左側に揃えることができます...次に、テキストビューでマージンとパディングと重力を使用して、あなたが望む場所に並べることができますイメージを超えたい。

于 2011-03-09T08:14:39.670 に答える
2

このため、1 つの TextView のみを使用しandroid:drawableLeft/Right/Top/Bottomて Image を TextView に配置できます。さらに、 TextView と drawable の間にパディングを使用できますandroid:drawablePadding=""

次のように使用します。

<TextView
    android:id="@+id/textAndImage"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:drawableBottom="@drawable/yourDrawable"
    android:drawablePadding="10dp" 
    android:text="Look at the drawable below"/>

これにより、余分な ImageView は必要ありません。TextView の複数の側で 2 つのドローアブルを使用することもできます。

これを使用することで直面する唯一の問題は、ドローアブルを ImageView のようにスケーリングできないことです。

于 2013-09-05T11:35:38.820 に答える
2

以下のコードを試してみてください。

  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="150dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@drawable/gallery1"/>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#7ad7d7d7"
        android:gravity="center"
        android:text="Juneja Art Gallery"
        android:textColor="#000000"
        android:textSize="15sp"/>
</RelativeLayout>
于 2016-01-02T08:57:04.757 に答える
2

TextView を使用して、その背景を使用したい画像に変更できます

于 2011-10-31T18:46:17.973 に答える
0

以下のコードが役立ちます

public class TextProperty {
    private int heigt;                              //读入文本的行数
    private String []context = new String[1024];    //存储读入的文本

    /*
     *@parameter wordNum
     *
     */
    public TextProperty(int wordNum ,InputStreamReader in) throws Exception {
        int i=0;
        BufferedReader br = new BufferedReader(in);
        String s;
        while((s=br.readLine())!=null){
            if(s.length()>wordNum){
                int k=0;
                while(k+wordNum<=s.length()){
                    context[i++] = s.substring(k, k+wordNum);
                    k=k+wordNum;
                }
                context[i++] = s.substring(k,s.length());
            }
            else{
                context[i++]=s;
            }
        }
        this.heigt = i;
        in.close();
        br.close();
    }


    public int getHeigt() {
        return heigt;
    }

    public String[] getContext() {

        return context;
    }
}
public class MainActivity extends AppCompatActivity {

    private Button btn;
    private ImageView iv;
    private final int WORDNUM = 35;  //转化成图片时  每行显示的字数
    private final int WIDTH = 450;   //设置图片的宽度

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv = (ImageView) findViewById(R.id.imageView);
        btn = (Button) findViewById(R.id.button);

        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                int x=5,y=10;
                try {
                    TextProperty tp = new TextProperty(WORDNUM, new InputStreamReader(getResources().getAssets().open("1.txt")));
                    Bitmap bitmap = Bitmap.createBitmap(WIDTH, 20*tp.getHeigt(), Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(bitmap);
                    Paint paint = new Paint();
                    paint.setColor(Color.WHITE);
                    paint.setTextAlign(Paint.Align.LEFT);
                    paint.setTextSize(20f);

                    String [] ss = tp.getContext();
                    for(int i=0;i<tp.getHeigt();i++){
                        canvas.drawText(ss[i], x, y, paint);
                        y=y+20;
                    }

                    canvas.save(Canvas.ALL_SAVE_FLAG);
                    canvas.restore();
                    String path = Environment.getExternalStorageDirectory() + "/image.png";
                    System.out.println(path);
                    FileOutputStream os = new FileOutputStream(new File(path));
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
                    //Display the image on ImageView.
                    iv.setImageBitmap(bitmap);
                    iv.setBackgroundColor(Color.BLUE);
                    os.flush();
                    os.close();
                }
                catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}```
于 2016-07-14T03:32:52.303 に答える