在图像视图中放置布局

时间:2016-03-10 17:48:48

标签: android imageview

我有一个像下面的相对布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="@drawable/follow_icon_circle">
<ImageView
    android:id="@+id/plus"
    android:layout_width="13dp"
    android:layout_height="13dp"
    android:src="@mipmap/follow_button_plus_icon"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="12dp"
    android:text="@string/txt_follow_button"
    android:layout_alignTop="@id/plus"
    android:layout_toRightOf="@id/plus"
    android:layout_marginRight="8dp"
    android:layout_marginLeft="5dp"
    android:textColor="@color/tab_menu_selected_color"/>
</RelativeLayout>

我想将此布局放在图像视图中。 像这样:imageView1.setImageResource(R.layout.RelativeLayout);

但它不起作用。 我怎样才能将这个外观放在我的imageView中?

1 个答案:

答案 0 :(得分:1)

为要作为图像获取的布局命名。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:id="@+id/relative"
android:background="@drawable/follow_icon_circle">

你需要创建一个如下的位图..

RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relative);
Bitmap bitmap = Bitmap.createBitmap(relativeLayout.getWidth(), relativeLayout.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
relativeLayout.draw(canvas);

并将其设置为您的imageview。

imageView1.setImageBitmap(bitmap);
相关问题