Android 自定義View時(shí)使用TypedArray配置樣式屬性詳細(xì)介紹
Android 自定義View時(shí)使用TypedArray配置樣式屬性詳細(xì)介紹
在自定義view時(shí)為了提高復(fù)用性和擴(kuò)展性,可以為自定義的view添加樣式屬性的配置,比如自定義圖片資源、文字大小、控件屬性等,就這需要用到TypedArray類,下面以一個(gè)自定義的可點(diǎn)擊擴(kuò)展和收縮的TextView為例記錄下這個(gè)類的簡單使用。
先上效果圖:

點(diǎn)擊以后為

再貼代碼:
1.自定義view類;
/**
* @title ExpandTextView
* @description 可擴(kuò)展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>
<!-- ******************************可擴(kuò)展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文件中定義樣式,可替換圖片資源;
<!-- 可擴(kuò)展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>
下面簡單描述下實(shí)現(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é)點(diǎn)中添加如下屬性:
xmlns:custom=http://schemas.android.com/apk/res/com.example.typedarraytest
格式:xmlns:自定義關(guān)鍵字(用于在控件中使用屬性,同android)=http://schemas.android.com/apk/res/包名
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
- Android的ImageButton當(dāng)顯示Drawable圖片時(shí)就不顯示文字
- Android開發(fā)仿咸魚鍵盤DEMO(修改版)
- Android自定義軟鍵盤的設(shè)計(jì)與實(shí)現(xiàn)代碼
- Android ListView適配器(Adapter)優(yōu)化方法詳解
- 實(shí)現(xiàn)一個(gè)Android鎖屏App功能的難點(diǎn)總結(jié)
- Android 實(shí)現(xiàn)界面刷新的幾種方法
- Android控件RefreshableView實(shí)現(xiàn)下拉刷新
- Android 自定義控件實(shí)現(xiàn)顯示文字的功能
相關(guān)文章
Android開發(fā)實(shí)現(xiàn)圖片圓角的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)圖片圓角的方法,涉及Android針對圖形圖像的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
Android?RecyclerView實(shí)現(xiàn)九宮格效果
這篇文章主要為大家詳細(xì)介紹了Android?RecyclerView實(shí)現(xiàn)九宮格效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
如何在Android中實(shí)現(xiàn)一個(gè)簡易的Http服務(wù)器
這篇文章主要介紹了如何在Android中實(shí)現(xiàn)一個(gè)簡易的Http服務(wù)器,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
Android TextView中文字通過SpannableString設(shè)置屬性用法示例
這篇文章主要介紹了Android TextView中文字通過SpannableString設(shè)置屬性用法,結(jié)合實(shí)例形式分析了TextView控件中SpannableString類相關(guān)屬性的使用技巧,需要的朋友可以參考下2016-08-08
Android開發(fā)之組件GridView簡單使用方法示例
這篇文章主要介紹了Android開發(fā)之組件GridView簡單使用方法,涉及Android GridView組件圖片瀏覽及保存圖片等相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
老生常談Listview中onItemClick中的各個(gè)參數(shù)(推薦)
下面小編就為大家?guī)硪黄仙U凩istview中onItemClick中的各個(gè)參數(shù)(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
Android PopupWindow實(shí)現(xiàn)遮罩層效果
這篇文章主要為大家詳細(xì)介紹了Android PopupWindow實(shí)現(xiàn)遮罩層效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10

