Android在布局中动态添加view的两种方法

一、说明

添加视图文件的时候有两种方式: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)

<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:core="http://schemas.android.com/apk/res/com.gxtag.gym" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="5dp">
 
    <imageview android:id="@+id/iv_head_album" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/defaulthead">
 
</imageview></linearlayout>

2、main.xml
<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ll_parent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical">
 
    <include android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/block_head_back">
 
</include></linearlayout>

3、DynamicViewActivity.xml
package com.gxtag.gym.ui;
 
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
 
import com.gxtag.gym.R;
import com.icq.app.widget.StatedButton;
 
public class DynamicViewActivity extends Activity implements OnClickListener{
 
    private Context mContext;
    private TextView mTv_title;
    private String title = "动态添加布局";
    private StatedButton mSbtn_back;
    private LinearLayout mLl_parent;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic);
        mContext=this;
        initView();
        mLl_parent.addView(addView1());
        mLl_parent.addView(addView2());
 
    }
     
    private void initView() {
        // TODO 初始化视图
        mLl_parent=(LinearLayout) findViewById(R.id.ll_parent);
        mTv_title = (TextView) findViewById(R.id.tv_title);
        mTv_title.setText(String.format(String.format(
                getResources().getString(R.string.title), title)));
        mSbtn_back = (StatedButton) findViewById(R.id.sbtn_navback);
        mSbtn_back.setOnClickListener(this);
         
         
    }
     
    private View addView1() {
        // TODO 动态添加布局(xml方式)
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( 
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
//      LayoutInflater inflater1=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//      LayoutInflater inflater2 = getLayoutInflater();
        LayoutInflater inflater3 = LayoutInflater.from(mContext);
        View view = inflater3.inflate(R.layout.block_gym_album_list_item, null);
        view.setLayoutParams(lp);
        return view;
         
    }
     
    private View addView2() {
        // TODO 动态添加布局(java方式)
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( 
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
        LinearLayout view = new LinearLayout(this); 
        view.setLayoutParams(lp);//设置布局参数 
        view.setOrientation(LinearLayout.HORIZONTAL);// 设置子View的Linearlayout// 为垂直方向布局 
        //定义子View中两个元素的布局 
        ViewGroup.LayoutParams vlp = new ViewGroup.LayoutParams( 
                ViewGroup.LayoutParams.WRAP_CONTENT, 
                ViewGroup.LayoutParams.WRAP_CONTENT); 
        ViewGroup.LayoutParams vlp2 = new ViewGroup.LayoutParams( 
                ViewGroup.LayoutParams.WRAP_CONTENT, 
                ViewGroup.LayoutParams.WRAP_CONTENT); 
          
        TextView tv1 = new TextView(this); 
        TextView tv2 = new TextView(this); 
        tv1.setLayoutParams(vlp);//设置TextView的布局 
        tv2.setLayoutParams(vlp2); 
        tv1.setText("姓名:"); 
        tv2.setText("李四"); 
        tv2.setPadding(calculateDpToPx(50), 0, 0, 0);//设置边距 
        view.addView(tv1);//将TextView 添加到子View 中 
        view.addView(tv2);//将TextView 添加到子View 中 
        return view; 
    }
 
    private int calculateDpToPx(int padding_in_dp){ 
        final float scale = getResources().getDisplayMetrics().density; 
        return  (int) (padding_in_dp * scale + 0.5f); 
    } 
     
 
    @Override
    public void onClick(View v) {
        // TODO 控件单击事件
        switch (v.getId()) {
        case R.id.sbtn_navback:
            this.finish();
            break;
        default:
            break;
        }
    }
 
}


  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 横向和竖向滚动的两种解决方法如下: 1. 使用 ScrollView 和 HorizontalScrollView ScrollView 和 HorizontalScrollViewAndroid 提供的两个滚动视图控件,可以用于实现竖向和横向的滚动。它们可以嵌套在布局,使得布局的内容可以滚动。 例如,如果想实现一个竖向滚动的布局,可以这样使用: ``` <ScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 布局内容 --> </ScrollView> ``` 如果想实现一个横向滚动的布局,可以这样使用: ``` <HorizontalScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 布局内容 --> </HorizontalScrollView> ``` 2. 使用 RecyclerView 和 LinearLayoutManager RecyclerView 和 LinearLayoutManager 是 Android 提供的两个用于实现列表布局控件,可以用于实现竖向和横向的滚动。RecyclerView 可以重复使用 View,从而提高了列表性能。 例如,如果想实现一个竖向滚动的列表,可以这样使用: ``` <androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent"/> LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(LinearLayoutManager.VERTICAL); recyclerView.setLayoutManager(layoutManager); ``` 如果想实现一个横向滚动的列表,可以这样使用: ``` <androidx.recyclerview.widget.RecyclerView android:layout_width="match_parent" android:layout_height="match_parent"/> LinearLayoutManager layoutManager = new LinearLayoutManager(this); layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL); recyclerView.setLayoutManager(layoutManager); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值