Original articles, reproduced please specify http://blog.csdn.net/leejizhou/article/details/51150116 Li Zizhou Blog
Set the length and width of the Android view we usually define in the XML of layout, so when do we need to dynamically set the properties of the view in the code? Let's look at the following UI design
In this UI design, the above ImageView is designed to be long and wide is 16:9 of the ratio, because the phone screen, the width of the picture is not determined, so in the XML is unable to set 16:9 scale ImageView, So to achieve this UI effect, you need to get the phone screen width at run time and then set the height of the ImageView at a scale of 16:9.
Take a look at the demo demo that dynamically changes view
- ImageView footage from Facebook team dribbble
The main implementation is to get the Layoutparams object through View.getlayoutparams (), then set the view's width and margin through the Layoutparams object, and then view Setlayoutparams again.
Let's take a look at the specific implementation steps
Layout a ImageView below 4 buttons for dynamic control of the picture
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns:tools =" Http://schemas.android.com/tools " android:layout_width = "match_parent" android:layout_height =" match_parent " android:orientation =" vertical " tools:context = " Com.leejz.viewchangedemo.MainActivity "; <imageview android: Layout_width = "match_parent" android:layout_ Height = "wrap_content" android:background
= "@mipmap/pic" android:id =" @+id/image "/> <button android:layout_ MarginTop = "12DP" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:text = "4:3" android:id = "@+id/ btn_43 "/> <button android:layout_ Width = "match_parent" android:layout_height< /span>= "wrap_content" android:text = "16:9" android:id = "@+id/ btn_169 "/> <button android:layout_ Width = "match_parent" android:layout_height< /span>= "wrap_content" android:text = "Margin left+" android:id = "@+id/btn_marginleft" /> <buttonandroid:layout_width="Match_parent"android:layout_height= "Wrap_content" Android:text="Margin right+"android:id="@+id/btn_marginright" /> </linearlayout>
Then the mainactivity, run the time to dynamically get the screen width, and then go to set the height of ImageView, specific reference code comments
/** * Blog:www.lijizhou.com * * Public class mainactivity extends appcompatactivity implements View . Onclicklistener { PrivateImageView image;PrivateButton btn_43,btn_169;PrivateButton Btn_marginleft,btn_marginright;//In order to change ImageView, get Layoutparmas object //ImageView parent group is linearlayout so use linearlayout.layoutparams here //You can also use a generic viewgroup.layoutparams but with fewer supported propertiesLinearlayout.layoutparams params;//imageview from the left margin Private intLeft//imageview distance from right margin Private intRight@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); Initview (); } Public void Initview() {image= (ImageView) Findviewbyid (r.id.image); Params= (Linearlayout.layoutparams) image.getlayoutparams (); btn_43= (Button) Findviewbyid (r.id.btn_43); btn_169= (Button) Findviewbyid (r.id.btn_169); btn_marginleft= (Button) Findviewbyid (r.id.btn_marginleft); btn_marginright= (Button) Findviewbyid (r.id.btn_marginright); Btn_43.setonclicklistener ( This); Btn_169.setonclicklistener ( This); Btn_marginleft.setonclicklistener ( This); Btn_marginright.setonclicklistener ( This); }@Override Public void OnClick(View v) {Switch(V.getid ()) { CaseR.id.btn_43:params.height=getscreenwidth () *3/4; Image.setlayoutparams (params); Break; CaseR.id.btn_169:params.height=getscreenwidth () *9/ -; Image.setlayoutparams (params); Break; CaseR.id.btn_marginleft://Left margin increase can be directly using PX or conversion using DP unitsPARAMS.LEFTMARGIN=DP2PX ( This, left+=8); Image.setlayoutparams (params); Break; CaseR.ID.BTN_MARGINRIGHT:PARAMS.RIGHTMARGIN=DP2PX ( This, right+=8); Image.setlayoutparams (params); Break; } }//Get run Screen width Public int Getscreenwidth() {displaymetrics DM =NewDisplaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);//Width dm.widthpixels //Height dm.heightpixels returnDm.widthpixels; }//DP turn px Public Static int dp2px(Context context,floatDpvalue) {Final floatScale = Context.getresources (). Getdisplaymetrics (). density;return(int) (Dpvalue * scale +0.5f); }//PX Transfer DP Public Static int PX2DP(Context context,floatPxvalue) {Final floatScale = Context.getresources (). Getdisplaymetrics (). density;return(int) (Pxvalue/scale +0.5f); }}
Ok, a small example of dynamic change of view is implemented, the code is very simple, the Layoutparams object can not only support changing the width and height margin also support dynamic settings weight and so on a series of operations, you need to explore the implementation:)
This source code http://download.csdn.net/detail/leejizhou/9490898
Android code dynamically changes the view's properties