导航:首页 > 操作系统 > 动态添加视图android

动态添加视图android

发布时间:2024-06-10 22:00:13

1. android动态添加自定义TextView后如何更新某一个TextView的内容

public class MainActivity extends Activity { private int index; private TextView mTextView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mTextView = new TextView(this); setContentView(mTextView); new DataThread().start(); } private String getWebData() { return String.valueOf(++index); } private Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { mTextView.setText((String)msg.obj); }; }; private class DataThread extends Thread { @Override public void run() { for(int i = 0; i < 4; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { } final String data = getWebData(); // 只能在主线程中修改ui控件 mHandler.sendMessage(mHandler.obtainMessage(0, data)); } } } }

2. android 如何动态布局自定义view,不用XML.

可以直接new View来得到View对象来实现代码布局。以下为示例代码:
1.绝对布局
AbsoluteLayout abslayout=new AbsoluteLayout (this);
setContentView(abslayout);
Button btn1 = new Button(this);
btn1.setText(”this is a button”);
btn1.setId(1);
AbsoluteLayout.LayoutParams lp1 =
new AbsoluteLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0,100);
abslayout.addView(btn1, lp1);

2.相对布局
RelativeLayout relativeLayout = new RelativeLayout(this);
setContentView(relativeLayout);
AbsoluteLayout abslayout=new AbsoluteLayout (this);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
relativeLayout.addView(abslayout ,lp1);

3.线性布局
LinearLayout ll = new LinearLayout(this);
EditText et = new EditText();
ll.addView(et);
//动态添加布局的方法1. LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null); setContentView(ll); LinearLayout ll2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,ll); //这样 main2 作为 main1的子布局 加到了 main1的 根节点下
//动态添加布局的方法2 addView. LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null); setContentView(ll); LinearLayout ll2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,null); ll.addView(ll2);

3. android中怎么动态的加载一个布局

由于前段时间项目需要,需要在一个页面上加载根据不同的按钮加载不同的布局页面,当时想到用 tabhot 。不过美工提供的界面图完全用不上tabhot ,所以想到了动态加载的方法来解决这一需求。在这里我整理了一下,写了一个 DEMO 希望大家以后少走点弯路。

首先,我们先把界面的框架图画出来,示意图如下:

4. 关于Android动态布局添加和删除View的问题……

java">{

privateLinearLayoutlayout;
privateTextViewtextView;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout=newLinearLayout(this);//变量layout是该Activity的成员变量(privateLinearLayoutlayout)
layout.setOrientation(LinearLayout.VERTICAL);//设置layout布局方向为垂直
setContentView(layout);

//接下来向layout中添加TextView
textView=newTextView(this);
textView.setText("ThisIsaTextView");
layout.addView(textView);
}
@Override
protectedvoidonResume(){
//TODOAuto-generatedmethodstub
layout.removeView(textView);
super.onResume();
}

}

但是Activity在启动的时候调用onCreate()之后也会调用onResume()方法,所以进入程序也看不到textview了

5. android涓娣诲姞澶撮儴瑙嗗浘镄勫姛鑳芥庝箞瀹炵幇

ListView鏄链塧ddHeaderView鍜 addFooterView涓や釜鏂规硶镄.
浣嗘槸浣滀负瀹樻柟鎺ㄨ崘镄凩istView镄勫崌绾х増RecyclerView缂烘棤娉曞疄鐜拌繖涓や釜鏂规硶銆
闾d箞濡傛灉浣跨敤RecyclerView瀹炵幇杩欎袱涓鏂规硶镄勬晥鏋滆ユ庝箞锅氩憿锛
缃戜笂镆ヨ浜嗗緢涔咃纴璇曡繃钖勭嶅悇镙风殑瀹炵幇鏂瑰纺锛岀粓浜庤╂垜鍙戠幇涓涓杩树笉阌欑殑瀹炵幇鏂规硶锛岄偅涔埚氨缁椤ぇ瀹舵帹钻愪竴涓嬨
椤圭洰鍦板潃锛埚埆浜哄啓镄勶纴闱炲崥涓荤殑锛https://github.com/jczmdeveloper/XCRecyclerView
鎴戠湅浜嗕笅杩欎釜婧愮爜锛屽緢绠鍗曪纴鍗冲啓浜嗕竴涓缁ф圹RecyclerView镄勬带浠讹纴镊宸卞疄鐜瘾ddHeaderView鍜宎ddFooterView涓や釜鏂规硶

?
code

package com.xqx.com.recyclerviewheaderdemo;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class XCRecyclerView extends RecyclerView{

private ArrayList<View> mHeaderViews = new ArrayList<>();
private ArrayList<View> mFooterViews = new ArrayList<>();
private RecyclerView.Adapter mAdapter;
private RecyclerView.Adapter mWrapAdapter;
private static final int TYPE_HEADER = -101;
private static final int TYPE_FOOTER = -102;
private static final int TYPE_LIST_ITEM = - 103;
public XCRecyclerView(Context context) {
this(context, null);
}
public XCRecyclerView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public XCRecyclerView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context){

}

@Override
public void setAdapter(Adapter adapter) {
mAdapter = adapter;
mWrapAdapter = new WrapAdapter(mHeaderViews, mFooterViews, adapter);
super.setAdapter(mWrapAdapter);
mAdapter.registerAdapterDataObserver(mDataObserver);
}
public void addHeaderView(View view){
mHeaderViews.clear();
mHeaderViews.add(view);
}
public void addFooterView(View view){
mFooterViews.clear();
mFooterViews.add(view);
}
public int getHeaderViewsCount(){
return mHeaderViews.size();
}
public int getFooterViewsCount(){
return mFooterViews.size();
}
private final RecyclerView.AdapterDataObserver mDataObserver = new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
mWrapAdapter.notifyDataSetChanged();
}

@Override
public void onItemRangeChanged(int positionStart, int itemCount) {
mWrapAdapter.notifyItemRangeChanged(positionStart, itemCount);
}

// @Override
// public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
// mWrapAdapter.notifyItemRangeChanged(positionStart, itemCount, payload);
// }

@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
mWrapAdapter.notifyItemRangeInserted(positionStart, itemCount);
}

@Override
public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
mWrapAdapter.notifyItemMoved(fromPosition, toPosition);
}

@Override
public void onItemRangeRemoved(int positionStart, int itemCount) {
mWrapAdapter.notifyItemRangeRemoved(positionStart, itemCount);
}
};
private class WrapAdapter extends RecyclerView.Adapter<ViewHolder>{

private Adapter mAdapter;
private List<View> mHeaderViews;
private List<View> mFooterViews;
public WrapAdapter(List<View> headerViews,List<View> footerViews,Adapter adapter){
this.mAdapter = adapter;
this.mHeaderViews = headerViews;
this.mFooterViews = footerViews;
}

public int getHeaderCount(){
return this.mHeaderViews.size();
}
public int getFooterCount(){
return this.mFooterViews.size();
}
public boolean isHeader(int position){
return position >= 0 && position < this.mHeaderViews.size();
}
public boolean isFooter(int position){
return position < getItemCount() && position >= getItemCount() - this.mFooterViews.size();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType == TYPE_HEADER){
return new CustomViewHolder(this.mHeaderViews.get(0));
}else if(viewType == TYPE_FOOTER){
return new CustomViewHolder(this.mFooterViews.get(0));
}else{
return this.mAdapter.onCreateViewHolder(parent,viewType);
}
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if(isHeader(position)) return;
if(isFooter(position)) return;
int rePosition = position - getHeaderCount();
int itemCount = this.mAdapter.getItemCount();
if(this.mAdapter != null){
if(rePosition < itemCount){
Log.v("czm","rePosition/itemCount="+rePosition+"/"+itemCount);
this.mAdapter.onBindViewHolder(holder,rePosition);
return;
}
}
}
@Override
public long getItemId(int position) {
if (this.mAdapter != null && position >= getHeaderCount()) {
int rePosition = position - getHeaderCount();
int itemCount = this.mAdapter.getItemCount();
if (rePosition < itemCount) {
return this.mAdapter.getItemId(rePosition);
}
}
return -1;
}
@Override
public int getItemViewType(int position) {
if(isHeader(position)){
return TYPE_HEADER;
}
if(isFooter(position)){
return TYPE_FOOTER;
}
int rePosition = position - getHeaderCount();
int itemCount = this.mAdapter.getItemCount();
if(rePosition < itemCount){
return this.mAdapter.getItemViewType(position);
}
return TYPE_LIST_ITEM;
}
@Override
public int getItemCount() {
if(this.mAdapter != null){
return getHeaderCount() + getFooterCount() + this.mAdapter.getItemCount();
}else{
return getHeaderCount() + getFooterCount();
}
}

@Override
public void registerAdapterDataObserver(AdapterDataObserver observer) {
if(this.mAdapter != null){
this.mAdapter.registerAdapterDataObserver(observer);
}
}

@Override
public void unregisterAdapterDataObserver(AdapterDataObserver observer) {
if(this.mAdapter != null){
this.mAdapter.unregisterAdapterDataObserver(observer);
}
}

private class CustomViewHolder extends ViewHolder{

public CustomViewHolder(View itemView) {
super(itemView);
}
}
}
}

XCRecyclerView
浣跨敤鏂规硶github閲屼篃鍐欑殑娓呮竻妤氭氱殑
?
code

private MyAdapter mAdapter;
private XCRecyclerView mRecyclerView;
private List<String> mData;
private View mHeaderView;
private View mFooterView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mData = new ArrayList<String>();
for(int i = 0; i < 10 ;i++){
mData.add("item_" + i);
}
mAdapter = new MyAdapter(mData);
mRecyclerView = (XCRecyclerView) findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mHeaderView = LayoutInflater.from(this).inflate(R.layout.layout_header,mRecyclerView,false);
mFooterView = LayoutInflater.from(this).inflate(R.layout.layout_footer,mRecyclerView,false);
mRecyclerView.addHeaderView(mHeaderView);
mRecyclerView.addFooterView(mFooterView);
mRecyclerView.setAdapter(mAdapter);
}

娉ㄦ剰镣癸细
addHeaderView涔嫔悗 鍒楄〃镄勬暟鎹鍧愭爣鍗崇浉搴斿彂鐢熷彉鍖栵紒鍗砤ddHeadView涓娆★纴鍒楄〃绗涓涓鏁版嵁镄勪笅鍧愭爣+1锛0-->1锛

阅读全文

与动态添加视图android相关的资料

热点内容
程序员到公司当领导 浏览:223
用算法控制玩家的行为 浏览:482
androidsdk17下载 浏览:792
怎么给单独表格添加密码 浏览:12
下载压缩密码 浏览:259
android系统上编程 浏览:469
单片机模拟i2c从机 浏览:237
教育年报系统服务器如何开启 浏览:840
对称密钥加密后的长度 浏览:292
微制造编程软件下载 浏览:107
旋住宿酒店用哪个App最好 浏览:60
三菱编程中怎么创建子程序 浏览:199
在单片机温度输入采集信号有 浏览:684
电脑云服务器同步 浏览:418
方舟生存进化手游版怎么转服务器 浏览:90
哪个app可以听小说 浏览:160
网络发送数据如何加密 浏览:201
教材完全解读pdf 浏览:820
什么是多台服务器 浏览:36
菜鸟音乐编辑app哪个好 浏览:548