Android氣泡效果實現(xiàn)方法
本文實例講述了Android氣泡效果實現(xiàn)方法。分享給大家供大家參考,具體如下:
最近在看以前在eoe上收藏的一些源代碼,準備將這些代碼加上一些自己的注釋,然后貼出來,方便自己日后查閱,和剛?cè)腴T的人來學習。
今天先看一個氣泡窗口,先看一下效果圖和目錄結構,然后再上代碼

通過第一幅圖,我們可以看到一個氣泡窗口,這個窗口的結構是里面有2個TextView和1個氣泡的背景,這個2個TextView放在了overlay_pop.xml文件中,氣泡窗口是這個layout的背景,overlay_pop.xml的代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/pub_pop_bg4"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView android:id="@+id/bubble_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="#000" />
<!-- 我知道了 -->
<TextView android:id="@+id/bubble_btn"
android:gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/bubble_text"
android:textSize="20dp"
android:textColorHighlight="#2151E0"
android:text=""
android:textColor="#1A75B9" />
</RelativeLayout>
這是實現(xiàn)功能的全部代碼:
public class TestBubbleActivity extends Activity {
/** 全局屏幕的高和寬 */
private static int SCREEN_WIDTH = 0 ;
private static int SCREEN_HEIGHT = 0;
/**氣泡顯示的詳細內(nèi)容*/
private TextView tvBubContent = null;
/**顯示 我知道了 的提示*/
private TextView tvKnow = null;
/**氣泡view 里面包含2個TextView*/
private View bubbleView = null;
/**氣泡dialog*/
private Dialog bubbleAlert = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getDimension();
bubbleView = getLayoutInflater().inflate(R.layout.overlay_pop, null);
tvKnow = (TextView)bubbleView.findViewById(R.id.bubble_btn);
tvKnow.setText(Html.fromHtml("<u>"+"我知道了"+"</u>"));
tvBubContent = (TextView)bubbleView.findViewById(R.id.bubble_text);
tvBubContent.setText("上次程序異常退出,正在傳輸歷史數(shù)據(jù)...");
tvKnow.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
bubbleAlert.cancel();
}
});
int tmpWidth = SCREEN_WIDTH/5*3;
int tmpHeight =SCREEN_HEIGHT/8;
//設置TextView寬度
tvKnow.setMinWidth(tmpWidth);
tvBubContent.setMaxWidth(tmpWidth);
//以指定的樣式初始化dialog
bubbleAlert = new Dialog(this,R.style.bubble_dialog);
Window win = bubbleAlert.getWindow();//獲取所在window
LayoutParams params = win.getAttributes();//獲取LayoutParams
params.x = -(SCREEN_WIDTH/8);//設置x坐標
params.y = -tmpHeight;//設置y坐標
params.width = tmpWidth;
win.setAttributes(params);//設置生效
bubbleAlert.setCancelable(false);
bubbleAlert.setContentView(bubbleView);
bubbleAlert.show();
}
/**
* 獲取屏幕尺寸
*/
private void getDimension(){
/** 獲取屏幕的寬和高 */
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
SCREEN_WIDTH = dm.widthPixels;
SCREEN_HEIGHT = dm.heightPixels;
}
}
更多關于Android動畫及Activity操作相關內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)動畫技巧匯總》、《Android控件用法總結》及《Android編程之a(chǎn)ctivity操作技巧總結》
希望本文所述對大家Android程序設計有所幫助。
相關文章
Android 通過cmake的方式接入opencv的方法步驟
這篇文章主要介紹了Android 通過cmake的方式接入opencv的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-04-04
零基礎學習教程之Linux下搭建android開發(fā)環(huán)境
這篇文章主要介紹了Linux下搭建android開發(fā)環(huán)境,特別適合零基礎的同學學習,想要在Linux及ubuntu11.10下配置android4.0.3開發(fā)環(huán)境的朋友可以參考一下2015-12-12
讓Android中RadioGroup不顯示在輸入法上面的辦法
在Android開發(fā)中,發(fā)現(xiàn)一個問題,打開輸入法導致下面的radioGroup的位置發(fā)生了變化,被頂?shù)搅溯斎敕ǖ纳厦?,那么該如何解決呢?下面來看看。2016-08-08
View Controller Transition實現(xiàn)京東加購物車效果
這篇文章主要介紹了View Controller Transition實現(xiàn)京東加購物車效果,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02
淺談Android性能優(yōu)化之內(nèi)存優(yōu)化
Android的內(nèi)存優(yōu)化是性能優(yōu)化中很重要的一部分,本文將詳細介紹Android性能優(yōu)化之內(nèi)存優(yōu)化。2021-06-06
Android統(tǒng)一處理登錄后攜帶數(shù)據(jù)跳轉(zhuǎn)到目標頁面的方式
我們在開發(fā)的時候,一定會遇到頁面跳轉(zhuǎn),下面這篇文章主要給大家介紹了關于Android統(tǒng)一處理登錄后攜帶數(shù)據(jù)跳轉(zhuǎn)到目標頁面的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-06-06
Android 系統(tǒng)相機拍照后相片無法在相冊中顯示解決辦法
這篇文章主要介紹了Android 系統(tǒng)相機拍照后相片無法在相冊中顯示解決辦法的相關資料,需要的朋友可以參考下2016-12-12

