国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 自定義View時使用TypedArray配置樣式屬性詳細介紹

2019-12-12 04:42:18
字體:
來源:轉載
供稿:網友

 Android 自定義View時使用TypedArray配置樣式屬性詳細介紹

      在自定義view時為了提高復用性和擴展性,可以為自定義的view添加樣式屬性的配置,比如自定義圖片資源、文字大小、控件屬性等,就這需要用到TypedArray類,下面以一個自定義的可點擊擴展和收縮的TextView為例記錄下這個類的簡單使用。

先上效果圖:

點擊以后為


再貼代碼:

1.自定義view類;

/**  * @title ExpandTextView  * @description 可擴展TextView,可以通過設置ExpandTextViewStyle來自定義展開圖片、收起圖片和最小展示的行數  */ public class ExpandTextView extends LinearLayout implements OnClickListener {   /**    * 默認最少展示的行數    */   private int defaultMinLines;   /**    * 是否展開    */   private boolean mCollapsed = true;   /**    * 是否重新布局    */   private boolean mRelayout = false;    private View expandView;   private TextView expandText;   private ImageView expandImg;   private Drawable mExpandDrawable;   private Drawable mCollapseDrawable;    public ExpandTextView(Context context) {     this(context, null);   }    public ExpandTextView(Context context, AttributeSet attrs) {     super(context, attrs);     init(attrs);   }    private void init(AttributeSet attrs) {     expandView = LayoutInflater.from(getContext()).inflate(         R.layout.pt__expand_textview, null);     expandText = (TextView) expandView.findViewById(R.id.expand_text);     expandText.setOnClickListener(this);     expandImg = (ImageView) expandView.findViewById(R.id.expand_img);     expandImg.setOnClickListener(this);      TypedArray a = getContext().obtainStyledAttributes(attrs,         R.styleable.ExpandTextViewStyle);     // 自定義圖片資源     mExpandDrawable = getResources().getDrawable(         a.getResourceId(R.styleable.ExpandTextViewStyle_expand,             R.drawable.pt__ic_expand));     expandImg.setBackgroundDrawable(mExpandDrawable);     mCollapseDrawable = getResources().getDrawable(         a.getResourceId(R.styleable.ExpandTextViewStyle_collapse,             R.drawable.pt__ic_collapse));     // 自定義最小行數     defaultMinLines = a.getInt(         R.styleable.ExpandTextViewStyle_default_min_lines, 2);     a.recycle();      LinearLayout.LayoutParams params = new LayoutParams(         LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);     params.gravity = Gravity.CENTER;     addView(expandView, params);   }    @Override   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {     if (!mRelayout) {       super.onMeasure(widthMeasureSpec, heightMeasureSpec);       return;     }     mRelayout = false;     expandText.setMaxLines(Integer.MAX_VALUE);     expandImg.setVisibility(View.GONE);     super.onMeasure(widthMeasureSpec, heightMeasureSpec);     if (expandText.getLineCount() <= defaultMinLines) {       return;     }     if (mCollapsed) {       expandText.setMaxLines(defaultMinLines);     }     expandImg.setVisibility(View.VISIBLE);     super.onMeasure(widthMeasureSpec, heightMeasureSpec);   }    public void setText(CharSequence text) {     mRelayout = true;     expandText.setText(text);   }    public void setText(int resId) {     this.setText(getContext().getString(resId));   }    @Override   public void onClick(View view) {     if (expandImg.getVisibility() != View.VISIBLE) {       return;     }     mCollapsed = !mCollapsed;     expandImg.setBackgroundDrawable(mCollapsed ? mExpandDrawable         : mCollapseDrawable);     expandText         .setMaxLines(mCollapsed ? defaultMinLines : Integer.MAX_VALUE);   } } 

2.在res/values下添加的attrs.xml文件中定義樣式屬性;

<resources>    <!-- ******************************可擴展ExpandTextView樣式******************************* -->   <declare-styleable name="ExpandTextViewStyle">      <!-- 展開圖片 -->     <attr name="expand" format="reference" />     <!-- 關閉圖片 -->     <attr name="collapse" format="reference" />     <!-- 最小行數 -->     <attr name="default_min_lines" format="integer" />   </declare-styleable>  </resources> 

3.在res/values下的style.xml文件中定義樣式,可替換圖片資源;

<!-- 可擴展ExpandTextView樣式 -->   <style name="ExpandTextViewStyle">     <item name="expand">@drawable/pt__ic_expand</item>     <item name="collapse">@drawable/pt__ic_collapse</item>     <item name="default_min_lines">3</item>   </style> 

4.布局文件;

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:custom="http://schemas.android.com/apk/res/com.example.typedarraytest"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:paddingBottom="@dimen/activity_vertical_margin"   android:paddingLeft="@dimen/activity_horizontal_margin"   android:paddingRight="@dimen/activity_horizontal_margin"   android:paddingTop="@dimen/activity_vertical_margin" >    <com.example.typedarraytest.ExpandTextView     android:id="@+id/expand_text_view"     style="@style/ExpandTextViewStyle"     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:layout_margin="8dp"     custom:default_min_lines="2" />  </RelativeLayout> 

下面簡單描述下實現步驟:

1.先定義好attrs.xml文件;

2.在自定義view類中獲取定義的樣式屬性,下面這幾行代碼是關鍵:

TypedArray a = getContext().obtainStyledAttributes(attrs,     R.styleable.ExpandTextViewStyle); // 自定義圖片資源 mExpandDrawable = getResources().getDrawable(     a.getResourceId(R.styleable.ExpandTextViewStyle_expand,         R.drawable.pt__ic_expand)); expandImg.setBackgroundDrawable(mExpandDrawable); mCollapseDrawable = getResources().getDrawable(     a.getResourceId(R.styleable.ExpandTextViewStyle_collapse,         R.drawable.pt__ic_collapse)); // 自定義最小行數 defaultMinLines = a.getInt(     R.styleable.ExpandTextViewStyle_default_min_lines, 2); a.recycle(); 

3.既可以直接在style.xml中定義樣式然后使用,也可以在布局文件中配置屬性:

custom:default_min_lines="2"

要使用上面的屬性,需要在布局文件的根節點中添加如下屬性:

xmlns:custom=http://schemas.android.com/apk/res/com.example.typedarraytest

格式:xmlns:自定義關鍵字(用于在控件中使用屬性,同android)=http://schemas.android.com/apk/res/包名

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德安县| 永新县| 吉木萨尔县| 衡南县| 泽州县| 大城县| 蓝田县| 缙云县| 海盐县| 枝江市| 通许县| 灵台县| 深圳市| 达拉特旗| 曲沃县| 马关县| 洪泽县| 盐城市| 措勤县| 清新县| 丽水市| 米林县| 河间市| 临潭县| 平遥县| 兴义市| 翼城县| 呼和浩特市| 平原县| 合川市| 额敏县| 多伦县| 呼和浩特市| 南陵县| 双峰县| 吉林市| 剑阁县| 辽阳县| 丰宁| 定结县| 扎兰屯市|