Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Activity in Android to pop up a menu or window from the bottom

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the Android how to use Activity pop-up menu or window from the bottom of the relevant knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that you read this Android how to use Activity from the bottom pop-up menu or window article will have a harvest, let's take a look.

Step 1: design the xml layout of the pop-up window:

Step 2: create a SelectPicPopupWindow class that inherits the Activity class and implement the OnClickListener interface (you don't have to implement this excuse here, but according to your own needs and convenience). Other code implementations are the same as writing regular Activity, as shown below:

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) / / add the selection window scope listener to get the contact first, that is, no longer execute the onTouchEvent () function. When you click somewhere else, execute the onTouchEvent () function to destroy the Activity layout.setOnClickListener (new OnClickListener () {public void onClick (View v) {/ / TODO Auto-generated method stub Toast.makeText (getApplicationContext ()), "Tip: click outside the window to close the window!" , Toast.LENGTH_SHORT) .show ();}}); / / add buttons to listen for btn_cancel.setOnClickListener (this); btn_pick_photo.setOnClickListener (this); btn_take_photo.setOnClickListener (this);} / / implement the onTouchEvent touch screen function but destroy the Activity @ Override public boolean onTouchEvent (MotionEvent event) {finish (); return true when clicking on the screen. } 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 ();}}

Step 3: write the MainActivity class. Here, simply click to launch the MainActivity that you just want to implement the window:

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) / / add the text control to listen, click on the pop-up custom window tv.setOnClickListener (new OnClickListener () {public void onClick (View v) {startActivity (new Intent (MainActivity.this,SelectPicPopupWindow.class));}});}}

Step 4: note that the configuration of SelectPicPopupWindow by AndroidManifest.xml is different from the usual one. Add the android:theme attribute to the activity, as follows:

The copy code is as follows:

Step 5: the most important step to implement this example is to set the android:theme attribute style to achieve the desired effect, as follows:

@ anim/push_bottom_in @ anim/push_bottom_out @ style/AnimBottom @ nullstinct colors-frame-- > trueful colors-- whether it appears on activity-- > truth-- translucency-- > truth-- no title-- > @ androidcolorlue-- True-- transparentscape-- background transparent-- > truth-- ambiguity--

Step 6: the animation code when posting pop-up and destroying:

Push_bottom_in.xml

Push_buttom_out.xml

Note: these two xml need to be placed under the anim folder of res/anim

Step 7; the running effect is as shown in the figure:

The window that slips out during the Android 4.0 phone test may have a gap between the two sides of the screen.

If you want the width of this interface to fill the entire screen, you can use the

SetContentView (R.layout.activity_main); this is the end of the article on "how to use Activity in Android to pop-up menus or windows from the bottom". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use Activity in Android to pop up menus or windows from the bottom". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report