在自定義view時為了提高複用性和擴充性,可以為自定義的view添加樣式屬性的配置,比如自定義圖檔資源、文字大小、控件屬性等,就這需要用到TypedArray類,下面以一個自定義的可點選擴充和收縮的TextView為例記錄下這個類的簡單使用。
先上效果圖:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsISOyMzMzYTNzEDOwEDM2EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
點選以後為
再貼代碼:
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/包名"
歡迎通路浏覽,有更好的想法請留言交流。