国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android編程之include文件的使用方法

2020-04-11 10:59:38
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例分析了Android編程之include文件的使用方法。分享給大家供大家參考,具體如下:

記得很久以前,聽(tīng)一位大神說(shuō),程序員都很懶,不懶惰的程序員不是好程序員,當(dāng)時(shí)不明白什么意思.后來(lái)慢慢的懂得了它的意思,好的程序員不要做重復(fù)的工作.

我們?cè)赼ndroid的布局文件中,常會(huì)遇到一些相同的布局,每個(gè)頁(yè)面都寫,一是比較麻煩,二是一旦有修改還得改多個(gè)文件.這個(gè)時(shí)候我們就可以用到include了.

非常簡(jiǎn)單的使用,下面看代碼

include的文件scollandlisttitle.xml

<?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="wrap_content"  android:orientation="vertical"  >  <LinearLayout    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:orientation="horizontal"    android:background="#000000"    >    <TextView      android:id="@+id/txt_left_f"      android:layout_width="0dp"      android:layout_height="50dp"      android:layout_weight="1"      android:gravity="center"      android:text="文字"      android:clickable="true"      />    <TextView      android:id="@+id/txt_right_f"      android:layout_width="0dp"      android:layout_height="50dp"      android:layout_weight="1"      android:gravity="center"      android:text="圖片"      android:clickable="true"      />  </LinearLayout>  <View    android:id="@+id/view_line"    android:layout_width="50dp"    android:layout_height="1dp"    android:background="#FF0066"    /></LinearLayout>

再看調(diào)用它的頁(yè)面:scollandlistviewpager.xml

<?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:orientation="vertical"  android:id="@+id/vparent_layout">  <com.example.listsuspension.MyScrollView    android:id="@+id/vscrollView"    android:layout_width="match_parent"    android:layout_height="fill_parent"    android:fillViewport="true">    <FrameLayout      android:layout_width="match_parent"      android:layout_height="wrap_content">      <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical"        >        <ImageView          android:id="@+id/viamge"          android:layout_width="match_parent"          android:layout_height="wrap_content"          android:background="@drawable/pic"          android:scaleType="center"          />        <include          android:id="@+id/vtitle"          layout="@layout/scollandlisttitle"          />        <android.support.v4.view.ViewPager         android:id="@+id/viewpager"         android:layout_width="match_parent"         android:layout_height="fill_parent"         />      </LinearLayout>      <include          android:id="@+id/vtop_title"          layout="@layout/scollandlisttitle"          />    </FrameLayout>  </com.example.listsuspension.MyScrollView></LinearLayout>

上述include代碼部分就是兩個(gè)引用.加上之后,是不是就能看到include文件里的內(nèi)容顯示出來(lái)了.

這里有一個(gè)問(wèn)題,include里面的控件id是一樣的,這怎么辦呢.其實(shí)很簡(jiǎn)單,它們是被不同的include所包含,所以在使用findViewById的時(shí)候,從包含它的include里面取就行了.下面是代碼

titleLayout = (LinearLayout)findViewById(R.id.vtitle);toptitleLayout = (LinearLayout)findViewById(R.id.vtop_title);//viewPager = (ViewPager)findViewById(R.id.viewpager);//點(diǎn)擊按鈕事件txt_left_fc = (TextView)toptitleLayout.findViewById(R.id.txt_left_f);txt_left_fc.setOnClickListener(this);txt_right_fc = (TextView)toptitleLayout.findViewById(R.id.txt_right_f);txt_right_fc.setOnClickList

希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 楚雄市| 南安市| 庆城县| 高州市| 清水河县| 威海市| 古田县| 巴南区| 台东县| 大城县| 醴陵市| 驻马店市| 鸡泽县| 威远县| 东海县| 光山县| 顺义区| 通江县| 博白县| 郯城县| 儋州市| 海盐县| 乐都县| 抚州市| 华池县| 湘阴县| 苍梧县| 蓬莱市| 呼玛县| 光泽县| 泾川县| 文化| 佛冈县| 马山县| 辽阳县| 调兵山市| 九江市| 松原市| 格尔木市| 游戏| 安溪县|