0

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

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

有任何想法吗?

4

1 回答 1

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 代码相同(除了 root 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);
于 2013-10-16T23:37:07.230 回答