新手建立网站的步骤,广州市企业网站建设平台,Wordpress找不到外观选项,影楼招聘在安卓应用中#xff0c;有很多时候需要动态添加View。比如从后台获取商品列表#xff0c;根据商品数量在页面渲染对应数量的条目#xff0c;这时候就需要动态添加View。
1.动态添加View的方法
动态添加View有两种方法#xff1a;
由代码生成子View#xff1a;这种方式…在安卓应用中有很多时候需要动态添加View。比如从后台获取商品列表根据商品数量在页面渲染对应数量的条目这时候就需要动态添加View。
1.动态添加View的方法
动态添加View有两种方法
由代码生成子View这种方式很繁琐布局效果也不直观而且很多属性设置不了也可能是我没找对方法从xml读取布局然后动态设置信息推荐该方式配置xml时可以直接看到效果可设置的参数也更多。
接下来就用代码演示下如何使用这两种方式动态添加View。
注子View也可以写在父View中设置visible为GONE即可在createItem最后添加一行item.setVisibility(View.VISIBLE);。
2. 代码实现
父View布局activity_list.xml
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:orientationverticalScrollViewandroid:layout_widthmatch_parentandroid:layout_height480dpandroid:layout_gravitycenter_horizontal|topandroid:layout_margin10dpLinearLayoutandroid:idid/list_itemsandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationvertical //ScrollViewLinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:gravitycenter_horizontalButtonandroid:idid/list_switch_modelandroid:layout_width100dpandroid:layout_height70dpandroid:layout_gravitycenterandroid:layout_marginRight20dpandroid:textSwitch Model /Buttonandroid:idid/list_add_itemandroid:layout_width100dpandroid:layout_height70dpandroid:layout_gravitycenterandroid:layout_marginLeft20dpandroid:textAdd Item //LinearLayout
/LinearLayout子View布局activity_item.xml
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentLinearLayoutandroid:idid/itemandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_marginTop5dpandroid:layout_marginBottom5dpandroid:orientationhorizontalLinearLayoutandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:layout_gravitycenter_verticalandroid:layout_marginLeft10dpandroid:layout_marginRight10dpandroid:gravityright|centerandroid:orientationhorizontalImageViewandroid:idid/item_imgandroid:layout_width48dpandroid:layout_height48dpandroid:layout_marginLeft10dpandroid:layout_marginRight10dpandroid:padding10dpandroid:srcdrawable/common_item //LinearLayoutLinearLayoutandroid:layout_widthwrap_contentandroid:layout_heightmatch_parentandroid:layout_gravitycenterandroid:layout_marginLeft10dpandroid:layout_marginRight10dpandroid:gravityleft|centerandroid:orientationverticalTextViewandroid:idid/item_indexandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textindex: item nameandroid:textColorcolor/blackandroid:textSize6pt /TextViewandroid:idid/item_nameandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:textname: item nameandroid:textColorcolor/blackandroid:textSize6pt //LinearLayoutLinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:layout_gravitycenter_verticalandroid:layout_marginRight10dpandroid:gravityright|centerandroid:orientationhorizontalImageViewandroid:idid/item_editandroid:layout_width48dpandroid:layout_height48dpandroid:layout_marginRight10dpandroid:padding10dpandroid:srcdrawable/common_edit /ImageViewandroid:idid/item_deleteandroid:layout_width48dpandroid:layout_height48dpandroid:layout_marginRight10dpandroid:padding10dpandroid:srcdrawable/common_delete //LinearLayout/LinearLayout
/LinearLayoutactivity类
package org.tao.hetools.activities;import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;import androidx.activity.ComponentActivity;
import androidx.annotation.Nullable;import org.tao.hetools.R;
import org.tao.hetools.entities.ItemInfo;import java.util.ArrayList;
import java.util.List;public class DynamicViewActivity extends ComponentActivity {private int index 0;private boolean createViewFromXml true;private ListItemInfo itemInfos new ArrayList();private LinearLayout listItemsLayout;Overrideprotected void onCreate(Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_list);listItemsLayout findViewById(R.id.list_items);initButton();initDynamicView();}private void initDynamicView() {if (listItemsLayout null) {return;}listItemsLayout.removeAllViews();for (ItemInfo itemInfo : itemInfos) {listItemsLayout.addView(createViewFromXml ? createViewFromCode(itemInfo) : createViewFromCode(itemInfo));}}private void initButton() {findViewById(R.id.list_add_item).setOnClickListener(view - {if (listItemsLayout null) {return;}ItemInfo itemInfo new ItemInfo(index, name index, R.drawable.common_item);index;itemInfos.add(itemInfo);listItemsLayout.addView(createViewFromXml ? createItem(itemInfo) : createViewFromCode(itemInfo));});findViewById(R.id.list_switch_model).setOnClickListener(view - {itemInfos.clear();listItemsLayout.removeAllViews();createViewFromXml !createViewFromXml;});}/*** 在代码中生成布局作为子view的布局** param itemInfo item信息* return item view*/private View createViewFromCode(ItemInfo itemInfo) {// item开头的图标layoutImageView itemImage new ImageView(this);itemImage.setImageResource(R.drawable.common_item);itemImage.setMaxHeight(136);// 这个高度跟dp的换算需要根据屏幕分辨率重新计算。下同itemImage.setMaxWidth(136);itemImage.setLeft(10);itemImage.setRight(10);itemImage.setPadding(5, 5, 5, 5);itemImage.setAdjustViewBounds(true);LinearLayout imageLayout new LinearLayout(this);imageLayout.setOrientation(LinearLayout.HORIZONTAL);imageLayout.addView(itemImage);// item信息layoutTextView index new TextView(this);index.setText(index: itemInfo.getIndex());index.setTextSize(10);TextView name new TextView(this);name.setText(name: itemInfo.getName());name.setTextSize(10);LinearLayout infoLayout new LinearLayout(this);infoLayout.setOrientation(LinearLayout.VERTICAL);infoLayout.addView(index);infoLayout.addView(name);// 操作layoutImageView edit new ImageView(this);edit.setImageResource(R.drawable.common_edit);edit.setMaxHeight(136);edit.setMaxWidth(136);edit.setAdjustViewBounds(true);ImageView delete new ImageView(this);delete.setImageResource(R.drawable.common_delete);delete.setMaxHeight(136);delete.setMaxWidth(136);delete.setAdjustViewBounds(true);LinearLayout operateLayout new LinearLayout(this);operateLayout.setOrientation(LinearLayout.HORIZONTAL);operateLayout.addView(edit);operateLayout.addView(delete);// 子view的信息LinearLayout layout new LinearLayout(this);layout.setOrientation(LinearLayout.HORIZONTAL);layout.addView(imageLayout);layout.addView(infoLayout);layout.addView(operateLayout);edit.setOnClickListener(view - {// todo 此处处理edit的点击事件Log.i(itemInfoList: , itemInfos.toString());});delete.setOnClickListener(view - {itemInfos.remove(itemInfo);listItemsLayout.removeView(layout);});return layout;}/*** 从xml文件获取布局作为子view的布局** param itemInfo item信息* return item view*/private View createItem(ItemInfo itemInfo) {LinearLayout inflate (LinearLayout) LayoutInflater.from(this).inflate(R.layout.activity_item, null);LinearLayout item inflate.findViewById(R.id.item);((ImageView) item.findViewById(R.id.item_img)).setImageResource(itemInfo.getImageResourceId());((TextView) item.findViewById(R.id.item_index)).setText(index: itemInfo.getIndex());((TextView) item.findViewById(R.id.item_name)).setText(name: itemInfo.getName());item.findViewById(R.id.item_edit).setOnClickListener(view - {// todo 此处处理edit的点击事件Log.i(itemInfoList: , itemInfos.toString());});item.findViewById(R.id.item_delete).setOnClickListener(view - {itemInfos.remove(itemInfo);listItemsLayout.removeView(item);});((ViewGroup) item.getParent()).removeAllViews();return item;}
}