[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"/>