Android 實現頂層視窗、懸浮視窗

來源:互聯網
上載者:User

標籤:android   頂層視窗   懸浮視窗   

1、片1所示,在一個Android應用中,除了標題列和底層的ActionBar的地區,是我們可以操縱的UI地區,那是不是說我們就不能改變除了這兩個地區的UI呢?答案是否定的。

比如現在我們希望把一個View放在視窗的最低端顯示,通過hierarchyviewer工具我們可以發現最底層的ActionBar是在TestActivity布局的父視窗中設定的,那麼我們想通過setContentView(R.layout.activity_main)在activity_main布局檔案中設定就無法實現了,通過hierarchyviewer發現TestActivity布局的root是一個id為content的FrameLayout,那麼可以通過下面兩種方法來實現:

2、

private void testLayout(){View content = findViewById(android.R.id.content);ViewGroup parent = (ViewGroup)content.getParent().getParent();getLayoutInflater().inflate(R.layout.add_view, parent);mCloseBarContainer = (LinearLayout) parent.findViewById(R.id.close_bar_container1);}

3、

private void testWindow(){WindowManager mWm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);View l= getLayoutInflater().inflate(R.layout.add_view, null); WindowManager.LayoutParams mParams = new WindowManager.LayoutParams();  mParams.width=WindowManager.LayoutParams.MATCH_PARENT; mParams.height=200; mParams.gravity=Gravity.BOTTOM;mWm.addView(l, mParams);mCloseBarContainer = (LinearLayout) l.findViewById(R.id.close_bar_container1);}

通過WindowManager來實現,如果要實現當TestActivity退出時仍然顯示這個View的懸浮窗效果的話,只需要在一個背景Service中來添加View就可以了

4、add_view.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:id="@+id/close_bar_container1"        android:layout_width="fill_parent"        android:layout_height="88dip"        android:layout_weight="0"        android:layout_gravity="bottom"        android:clickable="false"        android:gravity="center"        android:background="#80ff0000">        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:id="@+id/close_bar"                       android:clickable="false"            android:gravity="center"            android:orientation="horizontal" >            <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:scaleType="center"                android:src="@drawable/ic_launcher"                 />            <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:gravity="center"            android:textSize="14.4sp"            android:textStyle="bold"            android:textColor="#ffffff"            android:text="This is Test!"            />                    </LinearLayout></LinearLayout>

5、


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.