Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use RelativeLayout in Android

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to use RelativeLayout in Android". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use RelativeLayout in Android.

Documentation for RelativeLayout:

Its inheritance structure is:

Java.lang.Object ↳ android.view.View ↳ android.view.ViewGroup ↳ android.widget.RelativeLayout

Let's create a new project in Eclipse to take a look at the use of relative layout manager RelativeLayout:

We declare the layout manager as RelativeLayout in main.xml, and then create two ImageView components to display two images, in which the layout_toRightOf property is set on the second ImageView component, that is, the setting is relative to the right side of a component, which is filled in the value of the component ID, so this means that our img2 relative to the img1 is on the right. By running the program below, we see the following effect:

Obviously, the second image is placed on the right side of the * * image. Add another TextView component to the code below:

This component is also very simple. We set the layout_below property to specify that it should be placed under the second image. Then run the program and we get the following display effect:

There is no problem. The text is indeed at the bottom of the second picture, but it shows that if the * image is smaller than the second picture, it will have an overlay effect. Let's adjust the position to have a look, and the adjustment code is as follows:

Instead of explaining the meaning of the code here, run it directly, and we see:

If the text overlay * image is displayed, you need to continue to set it:

Run the program again, and we can see the following effect:

The text is just below img1 and on the right side of img2. At this point, there is still some space on the lower side of the text and on the right side of the img2, so we place another Button component:

Run the program again, and we get the following effect:

Like other layout managers, we can control the relative layout manager through Java code. Let's first take a look at the RelativeLayout.LayoutParams documentation:

Its inheritance structure is:

Java.lang.Object ↳ android.view.ViewGroup.LayoutParams ↳ android.view.ViewGroup.MarginLayoutParams ↳ android.widget.RelativeLayout.LayoutParams

You just need to set some rules when you control the relative layout manager in the code, that is, the layout_toRightOf and layout_below we saw above. Let's take a look at the code:

Package org.ourpioneer; import android.app.Activity; import android.os.Bundle; import android.view.ViewGroup; import android.widget.EditText; import android.widget.RelativeLayout; public class RelativeLayoutDemoActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); super.setContentView (R.layout.main); / / read the existing layout manager RelativeLayout relativeLayout = (RelativeLayout) super .findViewById (R.id.rLayout) / / get relative layout manager rLayout RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams (ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT); / / set layout manager parameter params.addRule (RelativeLayout.BELOW, R.id.btn); / / set placement rule EditText edit = new EditText (this) / / create EditText component relativeLayout.addView (edit,params);}}

Before writing the code, we need to add the ID attribute, that is, rLayout, to our layout manager in main.xml, and then we can control it in the code. Here we continue to add components to the existing layout manager, that is, to put an edit box under the button, so we set the layout manager parameter to FILL_PARENT, which is to fill the entire screen. Then the rule is located on the lower side of the btn, and then the component is added to the layout manager

Thank you for your reading, the above is the content of "how to use RelativeLayout in Android", after the study of this article, I believe you have a deeper understanding of how to use RelativeLayout in Android, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report