這篇文章將為大家詳細講解有關(guān)怎么在Android中實現(xiàn)動態(tài)布局,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
三種布局情況(注意不是方式)
1、無xml : 一個父類布局包含一個子父類布局,子父類布局中包含ImageView
2、無xml : 只有一個父類布局包含一個ImageView
3、有xlm布局: 通過布局ID 來進行動態(tài)布局添加
總結(jié)了下其實步驟如下:
無xml布局:
1、setContentView()之前new一個需要的布局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()找到要進行添加的布局控件
之后的步驟和無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); //設(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 : 只有一個父類布局包含一個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 來進行動態(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>
關(guān)于怎么在Android中實現(xiàn)動態(tài)布局就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
名稱欄目:怎么在Android中實現(xiàn)動態(tài)布局-創(chuàng)新互聯(lián)
瀏覽路徑:http://chinadenli.net/article48/desjhp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗、做網(wǎng)站、微信公眾號、標簽優(yōu)化、網(wǎng)站排名、定制開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容