欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用Activity實(shí)現(xiàn)從底部彈出菜單或窗口的方法

 更新時(shí)間:2017年07月07日 10:15:39   作者:johennes  
這篇文章主要介紹了Android使用Activity實(shí)現(xiàn)從底部彈出菜單或窗口的方法,涉及Android布局、窗口、事件監(jiān)聽(tīng)、權(quán)限控制等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Android使用Activity實(shí)現(xiàn)從底部彈出菜單或窗口的方法。分享給大家供大家參考,具體如下:

這里使用activity實(shí)現(xiàn)彈出滑動(dòng)窗口或菜單,主要是使用了一些設(shè)置activity的樣式來(lái)實(shí)現(xiàn)彈出窗口和滑動(dòng)效果,實(shí)現(xiàn)如下:

第一步:設(shè)計(jì)要彈出窗口的xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:gravity="center_horizontal"
  android:orientation="vertical"
 >
<LinearLayout
  android:id="@+id/pop_layout"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:gravity="center_horizontal"
  android:orientation="vertical"
  android:layout_alignParentBottom="true"
   android:background="@drawable/btn_style_alert_dialog_background"
   >
  <Button
    android:id="@+id/btn_take_photo"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:layout_marginTop="20dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="拍照"
    android:background="@drawable/btn_style_alert_dialog_button"
    android:textStyle="bold"
     />
  <Button
    android:id="@+id/btn_pick_photo"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:layout_marginTop="5dip"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="從相冊(cè)選擇"
     android:background="@drawable/btn_style_alert_dialog_button"
     android:textStyle="bold"
     />
  <Button
    android:id="@+id/btn_cancel"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:layout_marginTop="15dip"
    android:layout_marginBottom="15dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="取消"
    android:background="@drawable/btn_style_alert_dialog_cancel"
    android:textColor="#ffffff"
    android:textStyle="bold"
    />
</LinearLayout>
</RelativeLayout>

第二步:創(chuàng)建SelectPicPopupWindow類繼承Activity類并實(shí)現(xiàn)OnClickListener接口(可以不用在這里實(shí)現(xiàn)這個(gè)借口,根據(jù)自己需要和方便實(shí)現(xiàn)),其他代碼實(shí)現(xiàn)跟編寫常規(guī)Activity一樣就OK,如下:

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class SelectPicPopupWindow extends Activity implements OnClickListener{
  private Button btn_take_photo, btn_pick_photo, btn_cancel;
  private LinearLayout layout;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.alert_dialog);
    btn_take_photo = (Button) this.findViewById(R.id.btn_take_photo);
    btn_pick_photo = (Button) this.findViewById(R.id.btn_pick_photo);
    btn_cancel = (Button) this.findViewById(R.id.btn_cancel);
    layout=(LinearLayout)findViewById(R.id.pop_layout);
    //添加選擇窗口范圍監(jiān)聽(tīng)可以優(yōu)先獲取觸點(diǎn),即不再執(zhí)行onTouchEvent()函數(shù),點(diǎn)擊其他地方時(shí)執(zhí)行onTouchEvent()函數(shù)銷毀Activity
    layout.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "提示:點(diǎn)擊窗口外部關(guān)閉窗口!",
            Toast.LENGTH_SHORT).show();
      }
    });
    //添加按鈕監(jiān)聽(tīng)
    btn_cancel.setOnClickListener(this);
    btn_pick_photo.setOnClickListener(this);
    btn_take_photo.setOnClickListener(this);
  }
  //實(shí)現(xiàn)onTouchEvent觸屏函數(shù)但點(diǎn)擊屏幕時(shí)銷毀本Activity
  @Override
  public boolean onTouchEvent(MotionEvent event){
    finish();
    return true;
  }
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_take_photo:
      break;
    case R.id.btn_pick_photo:
      break;
    case R.id.btn_cancel:
      break;
    default:
      break;
    }
    finish();
  }
}

第三步:編寫MainActivity類,這里很簡(jiǎn)單就是點(diǎn)擊啟動(dòng)剛才要實(shí)現(xiàn)窗口的MainActivity即可

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
public class MainActivity extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView tv = (TextView) this.findViewById(R.id.text);
    //把文字控件添加監(jiān)聽(tīng),點(diǎn)擊彈出自定義窗口
    tv.setOnClickListener(new OnClickListener() {
      public void onClick(View v) {
        startActivity(new Intent(MainActivity.this,SelectPicPopupWindow.class));
      }
    });
  }
}

第四步:這里要注意下AndroidManifest.xml對(duì)SelectPicPopupWindow的配置跟常規(guī)的不一樣為該activity改添加android:theme屬性,如下:

復(fù)制代碼 代碼如下:
<activity android:name=".SelectPicPopupWindow" android:theme="@style/MyDialogStyleBottom" />

第五步:這一步是實(shí)現(xiàn)本實(shí)例最重要的一部就是設(shè)置android:theme屬性樣式以實(shí)現(xiàn)本例所需要的效果,如下:

<style name="AnimBottom" parent="@android:style/Animation">
  <item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
  <item name="android:windowExitAnimation">@anim/push_bottom_out</item>
</style>
<style name="MyDialogStyleBottom" parent="android:Theme.Dialog">
  <item name="android:windowAnimationStyle">@style/AnimBottom</item>
  <item name="android:windowFrame">@null</item>
;!-- 邊框 -->
  <item name="android:windowIsFloating">true</item>
;!-- 是否浮現(xiàn)在activity之上 -->
  <item name="android:windowIsTranslucent">true</item>
;!-- 半透明 -->
  <item name="android:windowNoTitle">true</item>
;!-- 無(wú)標(biāo)題 -->
  <item name="android:windowBackground">@android:color/transparent</item>
;!-- 背景透明 -->
  <item name="android:backgroundDimEnabled">true</item>
;!-- 模糊 -->
</style>

第六步:在貼出彈出和銷毀時(shí)的動(dòng)畫效果代碼:

push_bottom_in.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
  <translate
    android:duration="200"
    android:fromYDelta="100%p"
    android:toYDelta="0"
   />
</set>

push_buttom_out.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑出式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
  <translate
    android:duration="200"
    android:fromYDelta="0"
    android:toYDelta="50%p"
  />
</set>

注意:這兩個(gè)xml需要放在res/anim的anim文件夾下

第七步;運(yùn)行效果如圖:

安卓4.0的手機(jī)測(cè)試時(shí)滑出的的窗口可能與整個(gè)屏幕兩側(cè)會(huì)有間隙
如果要讓此界面的寬度撐滿整個(gè)屏幕,可以在

setContentView(R.layout.activity_main);

之后加上

復(fù)制代碼 代碼如下:
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論