Android動(dòng)態(tài)布局使用詳解
本文為大家分享了Android動(dòng)態(tài)布局的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下

內(nèi)容如下:介紹多種實(shí)現(xiàn)動(dòng)態(tài)布局的方法,以及如何用代碼來調(diào)整View位置
這里只介紹三種布局情況(注意不是方式)
1、無xml : 一個(gè)父類布局包含一個(gè)子父類布局,子父類布局中包含ImageView
2、無xml : 只有一個(gè)父類布局包含一個(gè)ImageView
3、有xlm布局: 通過布局ID 來進(jìn)行動(dòng)態(tài)布局添加
總結(jié)了下其實(shí)步驟如下:
無xml布局:
1、setContentView()之前new一個(gè)需要的布局layout,再將layout放入setContentView()
2、new 出需要的控件設(shè)置好參數(shù)(id、text···)
3、new LayoutParams 設(shè)置好控件的大小、位置屬性(這里感覺和xml設(shè)置控件屬性是一樣的)
4、最后將params和控件放入之前new的layout即可
有xml布局:
1、setContentView()和以前一樣放入layout.xml
2、通過findViewById()找到要進(jìn)行添加的布局控件
之后的步驟和無xml布局的2、3、4一樣
代碼如下:
1、無xml : 一個(gè)父類布局包含一個(gè)子父類布局,子父類布局中包含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); //設(shè)置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 : 只有一個(gè)父類布局包含一個(gè)ImageView
RelativeLayout relativeLayout = new RelativeLayout(this); setContentView(relativeLayout); ImageView imageView = new ImageView(this); imageView.setId(2); imageView.setImageResource(R.mipmap.ic_launcher); //params 可以理解為 imageView的位置、大小參數(shù)集合 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 來進(jìn)行動(dòng)態(tài)布局添加
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>
是不是很簡(jiǎn)單啊,了解到原理后對(duì)以后一些需要?jiǎng)討B(tài)變化的布局操作起來就十分的方便了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Eclipse NDK遷移到Android Studio的方法示例
本篇文章主要介紹了Eclipse NDK遷移到Android Studio的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
Android自定義實(shí)現(xiàn)BaseAdapter的普通實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)BaseAdapter的普通實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-08-08
Android 圖片添加水印的實(shí)現(xiàn)方法
這篇文章主要介紹了Android 圖片添加水印的實(shí)現(xiàn)方法的相關(guān)資料,添加水印的原理就是在畫布Canvas上繪制圖形、圖片、文字等等, 得到你想要的效果圖片,需要的朋友可以參考下2017-07-07
說說在Android如何使用服務(wù)(Service)的方法
這篇文章主要介紹了說說在Android如何使用服務(wù)(Service)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
Android自定View實(shí)現(xiàn)滑動(dòng)驗(yàn)證效果的代碼
這篇文章主要介紹了Android自定View實(shí)現(xiàn)滑動(dòng)驗(yàn)證效果,代碼分為自定義屬性代碼和自定義view代碼及使用方法,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12
Android平臺(tái)生成二維碼并實(shí)現(xiàn)掃描 & 識(shí)別功能
這篇文章主要介紹了Android平臺(tái)生成二維碼并實(shí)現(xiàn)掃描 & 識(shí)別功能的相關(guān)資料,需要的朋友可以參考下2016-06-06
Android動(dòng)畫之小球擬合動(dòng)畫實(shí)例
這篇文章主要介紹了Android動(dòng)畫之小球擬合動(dòng)畫實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-07-07

