Android編程之include文件的使用方法
本文實(shí)例分析了Android編程之include文件的使用方法。分享給大家供大家參考,具體如下:
記得很久以前,聽一位大神說,程序員都很懶,不懶惰的程序員不是好程序員,當(dāng)時(shí)不明白什么意思.后來慢慢的懂得了它的意思,好的程序員不要做重復(fù)的工作.
我們在android的布局文件中,常會(huì)遇到一些相同的布局,每個(gè)頁面都寫,一是比較麻煩,二是一旦有修改還得改多個(gè)文件.這個(gè)時(shí)候我們就可以用到include了.
非常簡單的使用,下面看代碼
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)用它的頁面: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í)很簡單,它們是被不同的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實(shí)現(xiàn)APP自動(dòng)更新功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)APP自動(dòng)更新功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android之內(nèi)置和外置sdcard路徑顯示并且寫入數(shù)據(jù)的方法
今天小編就為大家分享一篇Android之內(nèi)置和外置sdcard路徑顯示并且寫入數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
android開發(fā)教程之間隔執(zhí)行程序(android計(jì)時(shí)器)
android開發(fā)中有些情況需要隔一段時(shí)間去執(zhí)行某個(gè)操作一次或者是每隔一段時(shí)間久執(zhí)行某個(gè)操作,下面是實(shí)現(xiàn)方法2014-02-02
Android編程之手機(jī)壁紙WallPaper設(shè)置方法示例
這篇文章主要介紹了Android編程之手機(jī)壁紙WallPaper設(shè)置方法,結(jié)合實(shí)例形式分析了Android手機(jī)壁紙WallPaper的相關(guān)設(shè)置與使用技巧,需要的朋友可以參考下2017-08-08
實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
這篇文章主要介紹了實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android仿高德首頁三段式滑動(dòng)效果的示例代碼
很多app都會(huì)使用三段式滑動(dòng),比如說高德的首頁和某寶等物流信息都是使用的三段式滑動(dòng)方式。本文將介紹如何實(shí)現(xiàn)這一效果,感興趣的可以學(xué)習(xí)一下2022-01-01
Android Studio發(fā)布項(xiàng)目到Jcenter倉庫步驟(圖文)
這篇文章主要介紹了Android Studio發(fā)布項(xiàng)目到Jcenter倉庫步驟(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10

