日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Android--添加子视图(addView和setView)

發布時間:2025/6/15 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android--添加子视图(addView和setView) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我們在添加視圖文件的時候有兩種方式,一種是通過在xml文件定義layout,另一種方式是在java代碼中動態生成布局文件。

在xml中定義的layout要想轉化為view,需要使用到LayoutInflater類。

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.login,?null);

3.添加視圖文件

舉個例子,假如定義了一個toast,則可以設置視圖文件

toast.setView(view);

====

現在給出一個常用的土司烤面包的例子--讓帶圖片和文本的面包居中顯示,看代碼:

其中主文件只放置了一個button,xml文件就不贅述。

[java] view plaincopyprint?
  • package?com.cn.query;??
  • ??
  • import?android.app.Activity;??
  • import?android.os.Bundle;??
  • import?android.view.Gravity;??
  • import?android.view.LayoutInflater;??
  • import?android.view.View;??
  • import?android.widget.Button;??
  • import?android.widget.ImageView;??
  • import?android.widget.LinearLayout;??
  • import?android.widget.TextView;??
  • import?android.widget.Toast;??
  • ??
  • import?com.androidquery.AQuery;??
  • ??
  • public?class?AQueryTest2?extends?Activity?{??
  • ????AQuery?aq?=?new?AQuery(this);??
  • ????private?Button?button;??
  • ??
  • ????protected?void?onCreate(Bundle?savedInstanceState)?{??
  • ????????//?TODO?Auto-generated?method?stub??
  • ????????super.onCreate(savedInstanceState);??
  • ????????setContentView(R.layout.test1);??
  • ????????aq.id(R.id.button1).visible().clicked(this,?"click");??
  • ????}??
  • ??
  • ????public?void?click()?{??
  • ????????//?動態生成布局視圖--適用于簡單布局??
  • ????????Toast?toast?=?new?Toast(AQueryTest2.this);??
  • ????????toast.setDuration(3000);??
  • ????????//?設置重心--讓toast居中顯示??
  • ????????toast.setGravity(Gravity.CENTER,?0,?0);??
  • ????????LinearLayout?ll?=?new?LinearLayout(AQueryTest2.this);??
  • ????????ImageView?iv?=?new?ImageView(AQueryTest2.this);??
  • ????????iv.setImageResource(R.drawable.icon1);??
  • ????????//?設置圖片內邊距,使textview顯示在右側,避免重疊??
  • ????????iv.setPadding(0,?0,?15,?0);??
  • ????????//?布局屬于ViewGroup,可以調用添加視圖方法??
  • ????????ll.addView(iv);??
  • ????????TextView?textview?=?new?TextView(AQueryTest2.this);??
  • ????????textview.setText("我是創建消息的提示框");??
  • ????????//??
  • ????????ll.addView(textview);??
  • ????????toast.setView(ll);??
  • ????????toast.show();??
  • ????}??
  • ??
  • ????public?void?click2()?{??
  • ????????//?動態生成布局視圖--適用于復雜UI布局??
  • ????????Toast?toast?=?new?Toast(AQueryTest2.this);??
  • ????????toast.setDuration(3000);??
  • ????????//?設置重心??
  • ????????toast.setGravity(Gravity.CENTER,?0,?0);??
  • ????????//?創建inflater??
  • ????????LayoutInflater?inflater?=?getLayoutInflater();??
  • ????????//?通過inflate方法將layout轉化為view??
  • ????????View?view?=?inflater.inflate(R.layout.toast,?null);??
  • ????????//?設置視圖--Toast繼承自Widget,不是容器,只能調用設置視圖方法??
  • ????????toast.setView(view);??
  • ????????toast.show();??
  • ????}??
  • }??
  • clcik()方法是動態生成的布局,就不多說了。注意ll.addView(iv)這里用的是addView,因為LinearLayout繼承自ViewGroup,所以是個容器,容器添加視圖則用addView().

    click2()方法時將layout定義在xml文件,然后通過LayoutInflater類的實例化對象 inflater調用inflate方法將layout轉化為view。注意toast.setView(),Toast是widget,不是容器,只能用setView()設置視圖。

    click2()方法中使用的布局文件:

    toast.xml

    [html] view plaincopyprint?
  • <?xml?version="1.0"?encoding="utf-8"?>??
  • <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
  • ????android:layout_width="match_parent"??
  • ????android:layout_height="match_parent"??
  • ????android:gravity="center_vertical|center_horizontal"??
  • ????android:orientation="horizontal"?>??
  • ??
  • ????<ImageView??
  • ????????android:id="@+id/imageview3"??
  • ????????android:layout_width="fill_parent"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:paddingBottom="0px"??
  • ????????android:paddingLeft="0px"??
  • ????????android:paddingRight="5px"??
  • ????????android:paddingTop="0px"??
  • ????????android:src="@drawable/icon1"?/>??
  • ??
  • ????<TextView??
  • ????????android:id="@+id/textview3"??
  • ????????android:layout_width="wrap_content"??
  • ????????android:layout_height="wrap_content"??
  • ????????android:text="消息提示"?/>??
  • ??
  • </LinearLayout>??


  • 除此之外上面還用到的Android Aquery輕量級插件。需要導入相應的包就可。

    效果截圖:





    總結

    以上是生活随笔為你收集整理的Android--添加子视图(addView和setView)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。