Android uses java code to set the layout and View width/height or adaptability. androidjava

Source: Internet
Author: User

Android uses java code to set the layout and View width/height or adaptability. androidjava

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


In android Code, set the vertical center of the control and the distance between the two controls.

LinearLayout phone_LayoutAccount = new LinearLayout (mContext );
LinearLayout. LayoutParams params = new LinearLayout. LayoutParams (LinearLayout. LayoutParams. FILL_PARENT, LinearLayout. LayoutParams. MATCH_PARENT );
Phone_LayoutAccount.setGravity (Gravity. CENTER_VERTICAL );
// Params.
Params. setMargins (10, 10, 10, 10 );
Phone_LayoutAccount.setLayoutParams (params );
This is a linear layout I wrote. You can just copy it. You can refer to the api documentation if you do not understand
LinearLayout. LayoutParams

Android Layout

I want to tell you that dip is automatically calculated as the corresponding pixel Based on the screen resolution, and the conversion ratio of different models is fixed.
Let's take a look at the three models you use. Their aspect ratio is 5/3, 3/2, and 4/3 respectively. You can see that in the same width, the height of your instance is the highest, so let's take a look at your layout.
For example, the screen height is dip. When the screen height exceeds dip, the screen will be squeezed out.

Solution
Method 1:
It is most convenient to reduce the height of each button. For example, set a 20dip. You know, the screen dip is also determined. If the screen dip is exceeded, a problem may occur.
Method 2:
Set a ScrollView outside RelativeLayout to avoid extrusion or deformation.
Method 3:
You can use a Linearlayout to install all four buttons. android: layout_weight = "1" is added to each button"
Highly adaptive. Do not write the height to death.

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.