【问题标题】:programatically add overlay icon in an activity以编程方式在活动中添加覆盖图标
【发布时间】:2013-10-16 22:12:01
【问题描述】:

我在 Google 中搜索了一段时间,但似乎找不到我想做的事情。我希望能够在不使用任何 xml 的情况下以编程方式将图标添加为活动中指定位置的叠加层。

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

有什么想法吗?

【问题讨论】:

    标签: android icons android-activity overlay


    【解决方案1】:

    这取决于您使用的布局。如果你使用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);
    

    【讨论】:

    • 如果你不知道父级的布局是什么(例如根活动?
    • 您想要 Facebook 聊天气泡或自定义 Rom 中的 Halo bar 之类的东西?
    • 我的原始帖子上有一个链接,它显示了我想要的内容。我希望能够在任何活动中显示某种图标(例如在任何活动的任何角落。我认为最好使用 FrameLayout
    • 您想要覆盖所有应用程序还是只覆盖您自己的应用程序?
    • 任何应用程序/活动。将图标视为库的一部分
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 2016-07-11
    相关资源
    最近更新 更多