Android 动态添加 可以横向滑动的布局

布局  main

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:id="@+id/ll_parent"
    tools:context="com.example.administrator.hertalsclloviewtest.MainActivity">

    <TextView
        android:id="@+id/tv_title"
        android:text="Hello World!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <HorizontalScrollView
        android:id="@+id/horsc"
        android:layout_width="match_parent"
        android:layout_height="100dp">

        <LinearLayout
            android:id="@+id/ll_file"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            >
        </LinearLayout>


    </HorizontalScrollView>
    <Button
        android:id="@+id/sbtn_navback"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

 

list_item 布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
   >
    <LinearLayout
        android:padding="15dp"
        android:gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_margin="15dp"
            android:id="@+id/iv_head_album"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher">
        </ImageView>
        <Button
            android:layout_margin="15dp"
            android:text="123456"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:layout_margin="15dp"
            android:text="1458945"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <View
            android:background="#f2f2"
            android:layout_width="5dp"
            android:layout_height="25dp"/>
    </LinearLayout>



</LinearLayout>

main activity

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import android.widget.LinearLayout.LayoutParams;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext=this;
        initView();
//        mLl_parent.addView(addView1());
//        mLl_parent.addView(addView2());

    }
    private Context mContext;
    private TextView mTv_title;
    private String title = "动态添加布局";
    private Button mSbtn_back;
    private LinearLayout mLl_parent;
    private LinearLayout ll_file;
    private HorizontalScrollView horsc;


    private void initView() {
        // TODO 初始化视图
         mLl_parent=(LinearLayout) findViewById(R.id.ll_parent);
        ll_file=(LinearLayout) findViewById(R.id.ll_file);
        mTv_title = (TextView) findViewById(R.id.tv_title);
        mTv_title.setText(title);
        mSbtn_back = (Button) findViewById(R.id.sbtn_navback);
        horsc = (HorizontalScrollView) findViewById(R.id.horsc);
        mSbtn_back.setOnClickListener(this);


    }


    private View addView1() {        // TODO 动态添加布局(xml方式)
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
//              LayoutInflater inflater1=(LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//              LayoutInflater inflater2 = getLayoutInflater();
        LayoutInflater inflater3 = LayoutInflater.from(mContext);
        View view = inflater3.inflate(R.layout.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 view) {
        switch (view.getId()) {
            case R.id.sbtn_navback:
                ll_file.addView(addView1());
//                ll_file.addView(addView2());
                break;
            default:
                break;
        }

    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值