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