Android編程之include文件的使用方法
本文實(shí)例分析了Android編程之include文件的使用方法。分享給大家供大家參考,具體如下:
記得很久以前,聽一位大神說,程序員都很懶,不懶惰的程序員不是好程序員,當(dāng)時(shí)不明白什么意思.后來慢慢的懂得了它的意思,好的程序員不要做重復(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)容顯示出來了.
這里有一個(gè)問題,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ì)有所幫助。
- Android抽象布局——include、merge 、ViewStub詳解
- Android中使用include標(biāo)簽和merge標(biāo)簽重復(fù)使用布局
- android使用include調(diào)用內(nèi)部組件的方法
- 解析android中include標(biāo)簽的使用
- Android開發(fā)之merge結(jié)合include優(yōu)化布局
- Android開發(fā)技巧之ViewStub控件惰性裝載
- 深入分析Android ViewStub的應(yīng)用詳解
- Android組件ViewStub基本使用方法詳解
- Android布局技巧之include、merge與ViewStub標(biāo)簽的巧用
相關(guān)文章
Android之ArcSlidingHelper制作圓弧滑動(dòng)效果
這篇文章主要介紹了Android之ArcSlidingHelper制作圓弧滑動(dòng)效果,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08
Android學(xué)習(xí)筆記45之gson解析json
JSON即JavaScript Object Natation,是一種輕量級(jí)的數(shù)據(jù)交換格式,采用完全獨(dú)立于語(yǔ)言的文本格式,為Web開發(fā)提供了一種理想的數(shù)據(jù)交換格式。通過本篇文章給大家介紹Android學(xué)習(xí)筆記45之gson解析json的相關(guān)內(nèi)容,對(duì)android gson解析json相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2015-12-12
ImageView點(diǎn)擊可變暗的實(shí)例代碼(android代碼技巧)
本文給大家分享一段實(shí)例代碼給大家介紹ImageView點(diǎn)擊可變暗的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-02-02
分享10個(gè)很棒的學(xué)習(xí)Android開發(fā)的網(wǎng)站
我推薦的網(wǎng)站,都是我在學(xué)習(xí)Android 開發(fā)過程中發(fā)現(xiàn)的好網(wǎng)站,給初學(xué)者一些建議,少走一些彎路2015-03-03
Flutter中跨組件數(shù)據(jù)傳遞的方法總結(jié)
Flutter中的數(shù)據(jù)傳遞一般包括:父->子,子->父,父->父,也就是說嵌套時(shí)的傳遞以及跨頁(yè)面的傳遞,本文整理了三種我們通常使用的方法,需要的可以參考一下2023-06-06
Android之仿美團(tuán)加載數(shù)據(jù)幀動(dòng)畫
本文主要介紹了Android仿美團(tuán)加載數(shù)據(jù)幀動(dòng)畫的實(shí)例方法。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04
Android自定義View實(shí)現(xiàn)內(nèi)存清理加速球效果
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)內(nèi)存清理加速球的效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android語(yǔ)音聲波控件 Android條形波控件
這篇文章主要為大家詳細(xì)介紹了Android語(yǔ)音聲波控件 Android條形波控件的制作方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
Android 中使用 AsyncTask 異步讀取網(wǎng)絡(luò)圖片
這篇文章主要介紹了Android 中使用 AsyncTask 異步讀取網(wǎng)絡(luò)圖片的相關(guān)資料,需要的朋友可以參考下2016-02-02

