導航:首頁 > 操作系統 > 動態添加視圖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相關的資料

熱點內容
三菱編程中怎麼創建子程序 瀏覽:199
在單片機溫度輸入採集信號有 瀏覽:680
電腦雲伺服器同步 瀏覽:416
方舟生存進化手游版怎麼轉伺服器 瀏覽:87
哪個app可以聽小說 瀏覽:160
網路發送數據如何加密 瀏覽:201
教材完全解讀pdf 瀏覽:820
什麼是多台伺服器 瀏覽:36
菜鳥音樂編輯app哪個好 瀏覽:547
人工魚群演算法matlab 瀏覽:82
演算法coursera 瀏覽:124
濰坊諸城DNS伺服器地址聯通 瀏覽:10
共享文件夾不顯示任務欄 瀏覽:251
唱歌給黨聽是哪個app的活動 瀏覽:499
bp演算法源代碼 瀏覽:648
發票伺服器怎麼選 瀏覽:387
哪個app陪伴運動 瀏覽:882
學編程每天六點起床 瀏覽:954
烏鴉搜索演算法復現 瀏覽:77
android3d切換 瀏覽:751