Android uses java code to set the layout, View width/height, or adaptive

Source: Internet
Author: User

Android uses java code to set the layout, View width/height, or adaptive

In the achat project, the length and width of the conversation content are set to adaptive, but if there is too much text content, the width is almost full. If you say a lot of content to the other party, the full screen is full text, so it is difficult to tell whether it is from what others say or what they say. Therefore, we need to apply the Adaptive Layout to another Width limit.

First, set layout_width/layout_height of layout to wrap_content in xml. Then, in the getView method, let the layout wrap_content (Why is wrap_content set in xml, how about setting it again? Because of the reuse of the layout, I will not talk about it much), WidgetController. setLayoutWidth (holder. lay_content, MarginLayoutParams. WRAP_CONTENT );

Then, after filling out the layout, let the layout not exceed 50% of the screen width. If the layout is exceeded, use this as the maximum width:

int w=(int)(DensityUtil.getScreenWidth()*0.5);if (WidgetController.getWidth(holder.lay_content)>w){WidgetController.setLayoutWidth(holder.lay_content,w);}



The following is the code for setLayoutWidth, which is quite useful:

/*** Set the View's attention (pixel). If it is set as a self-attention, it should be written into MarginLayoutParams. WRAP_CONTENT * @ param view * @ param width */public static void setLayoutWidth (View view, int width) {/* MarginLayoutParams margin = new MarginLayoutParams (view. getLayoutParams (); margin. setMargins (margin. leftMargin, y, margin. rightMargin, y + margin. height); // RelativeLayout. layoutParams layoutParams = new RelativeLayout. layoutParams (margin); // view. setLayoutParams (layoutParams); ViewGroup. marginLayoutParams layoutParams = newLayParms (view, margin); // RelativeLayout. layoutParams layoutParams = new RelativeLayout. layoutParams (margin); view. setLayoutParams (layoutParams); view. requestLayout (); */if (view. getParent () instanceof FrameLayout) {FrameLayout. layoutParams lp = (FrameLayout. layoutParams) view. getLayoutParams (); lp. width = width; view. setLayoutParams (lp); // view. setX (x); view. requestLayout ();} else if (view. getParent () instanceof RelativeLayout) {RelativeLayout. layoutParams lp = (RelativeLayout. layoutParams) view. getLayoutParams (); lp. width = width; view. setLayoutParams (lp); // view. setX (x); view. requestLayout ();} else if (view. getParent () instanceof LinearLayout) {LinearLayout. layoutParams lp = (LinearLayout. layoutParams) view. getLayoutParams (); lp. width = width; view. setLayoutParams (lp); // view. setX (x); view. requestLayout ();}}






Reprinted please indicate the source: http://blog.csdn.net/rocklee

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.