以编程方式在活动中添加叠加图标

时间:2013-10-16 22:12:02

标签: android icons android-activity overlay

我一直在谷歌搜索一下,但我似乎无法找到我想做的事情。我希望能够以编程方式将图标添加到指定位置的活动中,而不使用任何xml。

我的意思的一个例子:http://cdn9.staztic.com/app/a/2326/2326236/pollfish-demo-2-1-s-307x512.jpg

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

这取决于您使用的布局。如果您使用RelativeLayout,则可以这样做:

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:src="@android:drawable/ic_"/>
</RelativeLayout>

这与此Java代码(根RelativeLayout除外)相同:

RelativeLayout layout = (RelativeLayout) findViewById(R.id.main);
ImageView child = new ImageView(this);
child.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

child.setLayoutParams(params);
layout.addView(child);
相关问题