1. 程式人生 > >Android-模仿微信小視窗(Dialog對話方塊風格的視窗)

Android-模仿微信小視窗(Dialog對話方塊風格的視窗)

運用方法:

將顯示視窗的風格 設定為對話方塊風格即可

具體效果:


具體實現:

首先我們先定義佈局檔案:

<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/idtatabHost"
    android:layout_width="300dp"
    android:layout_height="500dp"
    android:layout_gravity="center"
    android:layout_weight="1">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/wechat"
        android:scaleType="fitXY"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="假的 WeChat"
        android:textSize="20dp"
        android:textColor="#ffffffff"/>
    <Button
        android:id="@+id/send"
        android:onClick="send"
        android:text="點我一下 有驚喜(嚇) 。。。"
        android:textColor="#ffffffff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

然後我再活動中照常設定監聽事件等方法:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void send(View source){
        finish();
    }
}

最重要的部分:

在未見的  mainfest.xml 中設定 活的的樣式為對話方塊風格

具體如下:

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Material.Dialog"
            tools:targetApi="lollipop">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

大功告成!