欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2016年11月11日 11:22:38   作者:Snail  
這篇文章主要介紹了Android 自定義View時使用TypedArray配置樣式屬性詳細(xì)介紹的相關(guān)資料,需要的朋友可以參考下

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

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

先上效果圖:

點擊以后為


再貼代碼:

1.自定義view類;

/** 
 * @title ExpandTextView 
 * @description 可擴展TextView,可以通過設(shè)置ExpandTextViewStyle來自定義展開圖片、收起圖片和最小展示的行數(shù) 
 */ 
public class ExpandTextView extends LinearLayout implements OnClickListener { 
  /** 
   * 默認(rèn)最少展示的行數(shù) 
   */ 
  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)); 
    // 自定義最小行數(shù) 
    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" /> 
    <!-- 關(guān)閉圖片 --> 
    <attr name="collapse" format="reference" /> 
    <!-- 最小行數(shù) --> 
    <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> 

下面簡單描述下實現(xiàn)步驟:

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

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

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)); 
// 自定義最小行數(shù) 
defaultMinLines = a.getInt( 
    R.styleable.ExpandTextViewStyle_default_min_lines, 2); 
a.recycle(); 

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

custom:default_min_lines="2"

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

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

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

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

  • Android監(jiān)聽軟鍵盤彈出與隱藏的兩種方法

    Android監(jiān)聽軟鍵盤彈出與隱藏的兩種方法

    本篇文章主要介紹了Android監(jiān)聽軟鍵盤彈出與隱藏的兩種方法,非常具有實用價值,需要的朋友可以參考下
    2017-04-04
  • 利用Flutter實現(xiàn)“孔雀開屏”的動畫效果

    利用Flutter實現(xiàn)“孔雀開屏”的動畫效果

    這篇文章主要給大家介紹了關(guān)于如何利用Flutter實現(xiàn)“孔雀開屏”的動畫效果,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Android 多種dialog的實現(xiàn)方法(推薦)

    Android 多種dialog的實現(xiàn)方法(推薦)

    下面小編就為大家分享一篇Android 多種dialog的實現(xiàn)方法(推薦),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Android中實現(xiàn)基本的短信攔截功能的代碼示例

    Android中實現(xiàn)基本的短信攔截功能的代碼示例

    這篇文章主要介紹了Android中實現(xiàn)基本短信攔截功能的代碼示例,這里之突出核心部分針對一個號碼,當(dāng)然程序擴充后可以制定更多攔截規(guī)則,需要的朋友可以參考下
    2016-04-04
  • Android QQ登錄界面繪制代碼

    Android QQ登錄界面繪制代碼

    這篇文章主要為大家詳細(xì)介紹了Android QQ登錄界面繪制代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • android fm單體聲和立體聲的切換示例代碼

    android fm單體聲和立體聲的切換示例代碼

    切換是需要在一定的條件下滿足才會進(jìn)行切換,切換的條件和電臺的信號強度RSSI、信號穩(wěn)定性CQI等等都有關(guān)系
    2013-06-06
  • android popwindow實現(xiàn)左側(cè)彈出菜單層及PopupWindow主要方法介紹

    android popwindow實現(xiàn)左側(cè)彈出菜單層及PopupWindow主要方法介紹

    PopupWindow可以實現(xiàn)浮層效果,主要方法有:可以自定義view,通過LayoutInflator方法;可以出現(xiàn)和退出時顯示動畫;可以指定顯示位置等感興趣的朋友可以了解下哦,希望本文對你學(xué)習(xí)android菜單相關(guān)開發(fā)有所幫助
    2013-01-01
  • OpenGL Shader實例分析(1)Wave效果

    OpenGL Shader實例分析(1)Wave效果

    這篇文章主要為大家詳細(xì)介紹了OpenGL Shader實例分析第一篇,Wave效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • Android自定義進(jìn)度條效果

    Android自定義進(jìn)度條效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義進(jìn)度條效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 最新評論