編輯:關於Android編程
本文實例分析了Android編程之include文件的使用方法。分享給大家供大家參考,具體如下:
記得很久以前,聽一位大神說,程序員都很懶,不懶惰的程序員不是好程序員,當時不明白什麼意思.後來慢慢的懂得了它的意思,好的程序員不要做重復的工作.
我們在android的布局文件中,常會遇到一些相同的布局,每個頁面都寫,一是比較麻煩,二是一旦有修改還得改多個文件.這個時候我們就可以用到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>
再看調用它的頁面: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代碼部分就是兩個引用.加上之後,是不是就能看到include文件裡的內容顯示出來了.
這裡有一個問題,include裡面的控件id是一樣的,這怎麼辦呢.其實很簡單,它們是被不同的include所包含,所以在使用findViewById的時候,從包含它的include裡面取就行了.下面是代碼
titleLayout = (LinearLayout)findViewById(R.id.vtitle); toptitleLayout = (LinearLayout)findViewById(R.id.vtop_title); //viewPager = (ViewPager)findViewById(R.id.viewpager); //點擊按鈕事件 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
希望本文所述對大家Android程序設計有所幫助。
微信朋友圈性格標簽怎麼弄 微信朋友圈性格標簽怎麼生成
打開微信朋友圈,就被一組微信款性格標簽刷爆,在別人眼裡你究竟是一個怎樣的人呢?踏實務實還是傲嬌?這麼好玩的微信朋友圈性格標簽怎麼弄?下面小編就告訴大家微信朋
Android基於Eclipse開發環境的搭建全總結
開始學習Android應用程序開發,首先碰到的就是開發環境的搭建。說實話此類文章網上很多,我想總結的有以下幾點,以示區分: 一.簡單的開發環境搭建
Android自定義組件ListPopWindow
先看一下效果:效果就是這樣,看一下實現,其實也沒多難,就是想開源出來供小伙伴們使用,如有不合理地方,希望大家多多指正。1.自定義PopWindow首先我們分析一下,這樣的
android一個注冊頁面和驗證碼的實現
先上效果圖: 驗證碼采用PHP返回的imageview來實現,xml如下: