Use of include files for Android programming

  • 2020-12-21 18:10:17
  • OfStack

This paper analyzes the use of include files for Android programming. To share for your reference, the details are as follows:

Remember a long time ago, listen to a great god said, programmers are lazy, not lazy programmers are not good programmers, at that time do not understand what the meaning. Then slowly understand what it means, good programmers do not do repetitive work.

In android's layout files, we often encounter 1 of the same layout, each page is written, 1 is more troublesome, 2 is 1 once there are changes to the multiple files. At this point we can use include.

Very simple to use, let's look at the code

scollandlisttitle.xml for include


<?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=" The 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=" The picture "
      android:clickable="true"
      />
  </LinearLayout>
  <View
    android:id="@+id/view_line"
    android:layout_width="50dp"
    android:layout_height="1dp"
    android:background="#FF0066"
    />
</LinearLayout>

Look at the page that calls it: 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>

The include code section above is two references. If you add this, you will see the contents of the include file displayed.

Here is a problem, include control id is 1, how to do. They are simply contained by different include, so when using findViewById, just take it from the include that contains it. Here's the code


titleLayout = (LinearLayout)findViewById(R.id.vtitle);
toptitleLayout = (LinearLayout)findViewById(R.id.vtop_title);
//viewPager = (ViewPager)findViewById(R.id.viewpager);
// Click button event 
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

I hope this article has been helpful in Android programming.


Related articles: