編輯:關於 Android 開發
這篇練習用兩種方式做出「對話框」效果,分別是傳統的 Dialog,以及更靈活的 PopupWindow。
Dialog 需要搭配自訂主題才能拿掉預設的白色背景方塊,做法是讓 windowBackground 設為 @null:
package com.example.tsh;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Dialog 實現對話框
Dialog dialog = new Dialog(this, R.style.Theme_Tsh_Dialog);
dialog.setContentView(R.layout.activity_two);
dialog.show();
}
}
對應的主題定義:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="https://schemas.android.com/apk/res/android">
<!-- 自定義對話框主題 -->
<style name="Theme.Tsh.Dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@null</item>
</style>
</resources>
PopupWindow 的好處是可以掛在某個 View 下方(像下拉選單),還能設定點擊外部自動關閉:
package com.example.tsh;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button dropDown = (Button) findViewById(R.id.bt_dropdown);
dropDown.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
View anchor = View.inflate(MainActivity.this, R.layout.activity_two, null);
final PopupWindow popupWindow = new PopupWindow(anchor,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
// 點擊空白隱藏
popupWindow.setTouchable(true);
popupWindow.setTouchInterceptor(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
popupWindow.dismiss();
return false;
}
});
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));
popupWindow.showAsDropDown(arg0);
}
});
}
}
主布局很簡單,一顆觸發按鈕即可:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一個界面" />
<Button
android:id="@+id/bt_dropdown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示下拉" />
</LinearLayout>
兩者差別在於:Dialog 適合置中的模態提示,PopupWindow 則更適合依附於某個控件的位置彈出,靈活度更高。
Android Studio 實戰:混淆、打包與驗證是否真的混淆成功
前言:近期在整合即時通訊(環信 SDK)時,官方文件要求加入以下 keep 規則: -keep class com.hyphenate.** {*;} -dontwarn
Volley 用法詳解:GET、POST、圖片請求與請求取消
Volley 把網路請求抽象成「隊列 + 請求物件」兩件事,介面乾淨、擴充容易。使用前提是先把它導入 Android Studio:File → Project Stru
Android 圖片平移、縮放與旋轉同步實現
Android 圖片平移、縮放與旋轉同步實現在圖片瀏覽類應用中,平移、縮放與旋轉是非常常見的交互需求。很多示例只實現單一手勢,但實際業務往往要求多種操作可以同時進行,並在
ViewPager 與 GridView 實作首頁導航分頁
ViewPager 與 GridView 實作首頁導航分頁 首頁的宮格導航若項目較多,常需要分頁顯示。本文以 ViewPager 搭配 GridView,模擬常見的首頁導