Android之自定义View的封装

来源:互联网 发布:微信淘宝链接转换工具 编辑:程序博客网 时间:2024/06/05 07:32

最近要做一个APP,界面效果是这样的


恩,其中涉及到6个类似的组合型布局。其中的单个布局,我们可以这么写:

Item_Home.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:background="@android:color/white"    android:gravity="center"    android:orientation="vertical"    android:padding="@dimen/padding"    android:descendantFocusability="blocksDescendants">    <ImageView        android:layout_width="wrap_content"        android:layout_height="0dp"        android:layout_weight="1"        android:adjustViewBounds="true"        android:src="@drawable/icon_home_other" />    <TextView        style="@android:style/TextAppearance.Medium"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="党史资料" /></LinearLayout>
其中

    android:descendantFocusability="blocksDescendants"
是为了阻止view继续向子view传递触摸事件。当view里面有可触摸的控件时候,虽然可以

       android:focusable="false"
,但是毕竟太麻烦,还是直接阻止掉比较爽快

恩,单个布局文件就25行,一点也不多微笑

怎么办?直接include?噢耶,无法访问子元素。那么我用代码的方式,往里面一个个添加?Oh no,Activity的代码已经够多了,我可不想让我的代码变得更加臃肿。

办法总是有的。捧读了鸿洋大神的http://blog.csdn.net/lmj623565791/article/details/45022631这篇文章之后,我找到了一种比较优雅的方式来解决这个问题。

聪明的你大概已经猜到我想说什么了。没错,就是把单个元素封装起来,然后通过直接在XML里边修改属性去更改图片和文字。

它看起来像这样:

<com.dreamstations.partyhistory.View.Item_Home                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_weight="1"                android:src="@drawable/icon_home_other"                android:text="党史资料" />

Oh,yes,这样看起来清爽多了~~~


然后我们一步一步操作,从自定义属性开始。我们的子布局里面需要修改什么,我们就用自己的代码给它对应起来

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="test">        <attr name="item_imgsrc" format="int" />        <attr name="item_text" format="string" />    </declare-styleable></resources>

然后新建我们的自定义View:

在Item_Home.java里面,我们可能会这么写:

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.test);                int imgsrc = ta.getInteger(R.styleable.item_imgsrc, -1);String text = ta.getString(R.styleable.item_text);        Log.e(TAG, "text = " + text + " , textAttr = " + textAttr);        ta.recycle();

等等,既然系统自己提供了各种属性,那还自定义干嘛,直接系统原装进口属性走起~

    private static final int[] mAttrs={android.R.attr.src,android.R.attr.text};
    TypedArray ta=getContext().obtainStyledAttributes(attrs,mAttrs);

现在的Item_Home.java

package com.dreamstations.partyhistory.View;import android.annotation.SuppressLint;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import com.dreamstations.partyhistory.R;/** * Created by 7YHong on 2015/11/28. */public class Item_Home extends LinearLayout{    private static final int[] mAttrs={android.R.attr.src,android.R.attr.text};    public Item_Home(Context context) {        this(context, null);    }    public Item_Home(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public Item_Home(Context context, AttributeSet attrs, int defStyleAttr) {        this(context, attrs, defStyleAttr, 0);    }    @SuppressLint("NewApi")    public Item_Home(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {        super(context, attrs, defStyleAttr, defStyleRes);        inflate(getContext(), R.layout.item_home, this);        LinearLayout layout= (LinearLayout) this.getChildAt(0);        TypedArray ta=getContext().obtainStyledAttributes(attrs,mAttrs);        ((ImageView)layout.getChildAt(0)).setImageResource(ta.getResourceId(0,-1));        ((TextView)layout.getChildAt(1)).setText(ta.getString(1));        ta.recycle();    }}
值得注意的是这一句:
LinearLayout layout= (LinearLayout) this.getChildAt(0);
因为我们inflate的layout是一个LinearLayout,所以不能直接在this上面getChild,第一次做的时候就掉这个坑了,一直报错,调试了很久尴尬

最后贴出act_home.xml的代码,整洁度+10086

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_marginBottom="8dp"        android:layout_weight="1">        <android.support.v4.view.ViewPager            android:id="@+id/home_vp"            android:layout_width="match_parent"            android:layout_height="match_parent" />        <LinearLayout            android:id="@+id/home_vp_dots"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignBottom="@id/home_vp"            android:layout_centerHorizontal="true"            android:layout_marginBottom="16dp"            android:orientation="horizontal" />    </RelativeLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="3"        android:orientation="vertical">        <!--行布局-->        <LinearLayout            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_marginBottom="8dp"            android:layout_weight="1"            android:orientation="horizontal">            <!--第一列-->            <com.dreamstations.partyhistory.View.Item_Home                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_marginRight="@dimen/divide"                android:layout_weight="1"                android:src="@drawable/icon_home_announcement"                android:text="专题"                android:onClick="clicktest"/>            <!--第二列-->            <com.dreamstations.partyhistory.View.Item_Home                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_weight="1"                android:src="@drawable/icon_home_notification"                android:text="红色旅游" />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_marginBottom="8dp"            android:layout_weight="1"            android:orientation="horizontal">            <!--第一列-->            <com.dreamstations.partyhistory.View.Item_Home                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_marginRight="@dimen/divide"                android:layout_weight="1"                android:src="@drawable/icon_home_status"                android:text="新闻" />            <!--第二列-->            <com.dreamstations.partyhistory.View.Item_Home                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_weight="1"                android:src="@drawable/icon_home_party"                android:text="史海回眸" />        </LinearLayout>        <LinearLayout            android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_marginBottom="8dp"            android:layout_weight="1"            android:orientation="horizontal">            <!--第一列-->            <com.dreamstations.partyhistory.View.Item_Home                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_marginRight="@dimen/divide"                android:layout_weight="1"                android:src="@drawable/icon_home_study"                android:text="党史天地" />            <!--第二列-->            <com.dreamstations.partyhistory.View.Item_Home                android:layout_width="0dp"                android:layout_height="match_parent"                android:layout_weight="1"                android:src="@drawable/icon_home_other"                android:text="党史资料" />        </LinearLayout>    </LinearLayout></LinearLayout>








0 0
原创粉丝点击