当前位置:   article > 正文

基于Android在布局中动态添加view的两种方法_layout.addview

layout.addview

一、说明

添加视图文件的时候有两种方式:1、通过在xml文件定义layout;2、java代码编写

二、前言说明

1.构造xml文件

2.LayoutInflater

提到addview,首先要了解一下LayoutInflater类。这个类最主要的功能就是实现将xml表述的layout转化为View的功能。为了便于理解,我们可以将它与findViewById()作一比较,二者都是实例化某一对象,不同的是findViewById()是找xml布局文件下的具体widget控件实例化,而LayoutInflater找res/layout/下的xml布局文件来实例化的。

(1)创建

LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);或

LayoutInflater inflater = LayoutInflater.from(Activity.this);或

LayoutInflater inflater = getLayoutInflater();

这三种方法本质是相同的。

(2)inflate()

用LayoutInflater.inflate() 将LayOut文件转化成VIew。

View view = inflater.inflate(R.layout.block_gym_album_list_item, null);

3.添加视图文件

三、步骤

1、通过在xml文件定义layout(block_gym_album_list_item.xml)

  1. <linearlayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical"
  6. android:padding="5dp"
  7. <imageview
  8. android:id="@+id/iv_head_album"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:src="@drawable/defaulthead"
  12. </imageview
  13. </linearlayout

复制

activity_dynamic

  1. <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:id="@+id/ll_parent"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical"
  6. <include
  7. android:layout_width="fill_parent"
  8. android:layout_height="wrap_content"
  9. layout="@layout/block_head_back"
  10. </include
  11. </linearlayout

复制

3、MainActivity

  1. package com.gxtag.gym.ui;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.View.OnClickListener;
  8. import android.view.ViewGroup;
  9. import android.widget.LinearLayout;
  10. import android.widget.LinearLayout.LayoutParams;
  11. import android.widget.TextView;
  12. import com.gxtag.gym.R;
  13. import com.icq.app.widget.StatedButton;
  14. public class MainActivityextends Activity implements OnClickListener{
  15. private Context mContext;
  16. private TextView mTv_title;
  17. private String title = "动态添加布局";
  18. private StatedButton mSbtn_back;
  19. private LinearLayout mLl_parent;
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.activity_dynamic);
  24. mContext=this;
  25. initView();
  26. mLl_parent.addView(addView1());
  27. mLl_parent.addView(addView2());
  28. }
  29. private void initView() {
  30. // TODO 初始化视图
  31. mLl_parent=(LinearLayout) findViewById(R.id.ll_parent);
  32. mTv_title = (TextView) findViewById(R.id.tv_title);
  33. mTv_title.setText(String.format(String.format(
  34. getResources().getString(R.string.title), title)));
  35. mSbtn_back = (StatedButton) findViewById(R.id.sbtn_navback);
  36. mSbtn_back.setOnClickListener(this);
  37. }
  38. private View addView1() {
  39. // TODO 动态添加布局(xml方式)
  40. LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
  41. LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); //LayoutInflater inflater1=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  42. // LayoutInflater inflater2 = getLayoutInflater();
  43. LayoutInflater inflater3 = LayoutInflater.from(mContext);
  44. View view = inflater3.inflate(R.layout.block_gym_album_list_item, null);
  45. view.setLayoutParams(lp);
  46. return view;
  47. }
  48. private View addView2() {
  49. // TODO 动态添加布局(java方式)
  50. LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
  51. LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
  52. LinearLayout view = new LinearLayout(this);
  53. view.setLayoutParams(lp);//设置布局参数
  54. view.setOrientation(LinearLayout.HORIZONTAL);// 设置子View的Linearlayout// 为垂直方向布局
  55. //定义子View中两个元素的布局
  56. ViewGroup.LayoutParams vlp = new ViewGroup.LayoutParams(
  57. ViewGroup.LayoutParams.WRAP_CONTENT,
  58. ViewGroup.LayoutParams.WRAP_CONTENT);
  59. ViewGroup.LayoutParams vlp2 = new ViewGroup.LayoutParams(
  60. ViewGroup.LayoutParams.WRAP_CONTENT,
  61. ViewGroup.LayoutParams.WRAP_CONTENT);
  62. TextView tv1 = new TextView(this);
  63. TextView tv2 = new TextView(this);
  64. tv1.setLayoutParams(vlp);//设置TextView的布局
  65. tv2.setLayoutParams(vlp2);
  66. tv1.setText("姓名:");
  67. tv2.setText("李四");
  68. tv2.setPadding(calculateDpToPx(50), 0, 0, 0);//设置边距
  69. view.addView(tv1);//将TextView 添加到子View 中
  70. view.addView(tv2);//将TextView 添加到子View 中
  71. return view;
  72. }
  73. private int calculateDpToPx(int padding_in_dp){
  74. final float scale = getResources().getDisplayMetrics().density;
  75. return (int) (padding_in_dp * scale + 0.5f);
  76. }
  77. @Override
  78. public void onClick(View v) {
  79. // TODO 控件单击事件
  80. switch (v.getId()) {
  81. case R.id.sbtn_navback:
  82. this.finish();
  83. break;
  84. default:
  85. break;
  86. }
  87. }
  88. }

以上这篇基于Android在布局中动态添加view的两种方法(总结)就是小编分享给大家的全部内容了,希望能给大家一个参考。

Demo:

public class MainActivity extends AppCompatActivity {
    private LinearLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        layout = findViewById(R.id.test);
        layout.addView(getView());
    }

    private View getView() {
        // TODO 动态添加布局(xml方式)
        FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                dip2px(this, 100),
                dip2px(this, 500));
        LayoutInflater inflater3 = LayoutInflater.from(this);
        View view = inflater3.inflate(R.layout.layout, null);
        view.setLayoutParams(lp);
        return view;
    }

    public static int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
}

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/225292
推荐阅读
相关标签
  

闽ICP备14008679号