[Android] enables popupwindow to display above the view and align horizontally with the view. androidpopupwindow

Source: Internet
Author: User

[Android] enables popupwindow to display above the view and align horizontally with the view. androidpopupwindow

First, let's talk nonsense. First, go:



Code:

public class MainActivity extends Activity implements OnClickListener{private Button showBtn1;private Button showBtn2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);showBtn1 = (Button) findViewById(R.id.showBtn1);showBtn2 = (Button) findViewById(R.id.showBtn2);showBtn1.setOnClickListener(this);showBtn2.setOnClickListener(this);}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubView popupView = LayoutInflater.from(this).inflate(R.layout.popup, null);PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);popupWindow.setBackgroundDrawable(new BitmapDrawable());popupView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);int popupWidth = popupView.getMeasuredWidth();int popupHeight =  popupView.getMeasuredHeight();int[] location = new int[2];switch (v.getId()) {case R.id.showBtn1:    v.getLocationOnScreen(location);    popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0]+v.getWidth()/2)-popupWidth/2,    location[1]-popupHeight);break;case R.id.showBtn2:    v.getLocationOnScreen(location);    popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0]+v.getWidth()/2)-popupWidth/2,    location[1]-popupHeight);break;default:break;}}}


Demo download: http://download.csdn.net/detail/u011494050/7787359


Android sets textView horizontal center display

First, I don't quite understand your problem.
Do you want to align the content in textView horizontally or the textView control horizontally in its parent layout?
1. Align the content in textView horizontally: android: gravity = "center_horizontal"
2. Align the textView control horizontally in its parent layout. android: layout_gravity = "center_horizontal"

How does android achieve horizontal vertical center of textview

<TextView
Android: id = "@ android: id/empty"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: layout_weight = "1"
Android: gravity = "center" // This sentence is horizontally centered.
Android: textColor = "#00CD00"
Android: textSize = "25px"/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.