Android code dynamically changes View attributes

Source: Internet
Author: User

Android code dynamically changes View attributes

We usually define the length and location of the Android View in Layout XML. When do we need to dynamically set the View attribute in the code? First look at the following uidesign

In this uidesign, the above ImageView is designed to be a ratio of 16: 9 in length and width. Because of the differences between mobile phone screens, the image width cannot be determined, therefore, you cannot set an ImageView with a ratio of in XML. To achieve this UI effect, you need to get the screen width of the mobile phone at runtime and set the height of the ImageView according to the ratio.

First, let's take a look at the Demo of dynamically changing the View. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Watermark + PGltZyBhbHQ9 "here write picture description" src = "http://www.bkjia.com/uploads/allimg/160417/0414026209-1.gif" title = "\"/>

The main implementation method is to get the LayoutParams object through View. getLayoutParams (), then set the width and margin of the View through the LayoutParams object, and then setLayoutParams the View.

The following describes the specific implementation steps.

Layout: four buttons Under an ImageView are used to dynamically control images.


  
      
   
    
    
    
   
  

Then MainActivity dynamically obtains the screen width during running, and then sets the ImageView height. For details, refer to the code comments.

/*** Blog: www.lijizhou.com */public class MainActivity extends AppCompatActivity implements View. onClickListener {private ImageView image; private Button btn_43, btn_169; private Button enabled, btn_marginright; // to change the imageview, The LayoutParmas object is obtained. // The ImageView parent Group is Linearlayout. layoutParams // you can also use a common ViewGroup. layoutParams does not support less LinearLayout attributes. layoutParams params; // The left margin of ImageView is private int left; // The right margin of ImageView is private int right; @ 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); listener (this) ;}@ Override public void onClick (View v) {switch (v. getId () {case R. id. btn_43: params. height = getScreenWidth () * 3/4; image. setLayoutParams (params); break; case R. id. btn_169: params. height = getScreenWidth () * 9/16; image. setLayoutParams (params); break; case R. id. btn_marginleft: // you can directly use PX to increase the left margin or use the DP unit params for conversion. leftMargin = dp2px (this, left + = 8); image. setLayoutParams (params); break; case R. id. btn_marginright: params. rightMargin = dp2px (this, right + = 8); image. setLayoutParams (params); break ;}// get the running screen width public int getScreenWidth () {DisplayMetrics dm = new DisplayMetrics (); getWindowManager (). getdefadisplay display (). getMetrics (dm); // The width of dm. widthPixels // height dm. heightPixels return dm. widthPixels;} // DP to convert PX public static int dp2px (Context context, float dpValue) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (dpValue * scale + 0.5f);} // convert PX to DP public static int px2dp (Context context, float pxValue) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (pxValue/scale + 0.5f );}}

 

 

OK, a small example of dynamically changing the View is implemented, and the code is very simple. LayoutParams object not only supports changing the width and height margin, but also supports dynamic setting of weight and a series of other operations, you need to explore and implement it yourself :)

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.