温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在Android中实现动态布局

发布时间:2021-03-26 17:07:23 来源:亿速云 阅读:274 作者:Leah 栏目:移动开发

这篇文章将为大家详细讲解有关怎么在Android中实现动态布局,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

三种布局情况(注意不是方式)

1、无xml : 一个父类布局包含一个子父类布局,子父类布局中包含ImageView

2、无xml : 只有一个父类布局包含一个ImageView

3、有xlm布局: 通过布局ID 来进行动态布局添加

总结了下其实步骤如下:

无xml布局:

1、setContentView()之前new一个需要的布局layout,再将layout放入setContentView()

2、new 出需要的控件设置好参数(id、text···)

3、new LayoutParams 设置好控件的大小、位置属性(这里感觉和xml设置控件属性是一样的)

4、最后将params和控件放入之前new的layout即可  

有xml布局:

1、setContentView()和以前一样放入layout.xml

2、通过findViewById()找到要进行添加的布局控件

之后的步骤和无xml布局的2、3、4一样

代码如下:

1、无xml : 一个父类布局包含一个子父类布局,子父类布局中包含ImageView

RelativeLayout relativeLayout = new RelativeLayout(this);
 setContentView(relativeLayout);
 
 RelativeLayout rl = new RelativeLayout(this);
 rl.setId(11);
 ImageView imageView = new ImageView(this);
 imageView.setId(1);
 imageView.setImageResource(R.mipmap.ic_launcher);
 
 RelativeLayout.LayoutParams lpRl = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
  ViewGroup.LayoutParams.WRAP_CONTENT);
 rl.setGravity(RelativeLayout.CENTER_IN_PARENT); //设置imageView 在 rl中的位置为居中
 rl.addView(imageView, lpRl);
 
 RelativeLayout.LayoutParams lpParent = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
  ViewGroup.LayoutParams.MATCH_PARENT);
 relativeLayout.addView(rl,lpParent);

2、无xml : 只有一个父类布局包含一个ImageView

RelativeLayout relativeLayout = new RelativeLayout(this);
 setContentView(relativeLayout);
 
 ImageView imageView = new ImageView(this);
 imageView.setId(2);
 imageView.setImageResource(R.mipmap.ic_launcher);
 //params 可以理解为 imageView的位置、大小参数集合
 RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
 params.addRule(RelativeLayout.CENTER_IN_PARENT);
 relativeLayout.addView(imageView,params);

3、有xlm布局: 通过布局ID 来进行动态布局添加

public class ThirdActivity extends AppCompatActivity {
 
 private LinearLayout mLinearLayout;
 
 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_third);
 
 mLinearLayout = (LinearLayout) findViewById(R.id.linear_layout);
 ImageView imageView = new ImageView(this);
 imageView.setImageResource(R.mipmap.ic_launcher);
 imageView.setId(31);
 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
 params.setMargins(150, 80, 10, 0);
 mLinearLayout.addView(imageView, params);
 }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/linear_layout"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
</LinearLayout>

关于怎么在Android中实现动态布局就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI