為了提高自定義視圖的可重用性和可伸縮性,可以為自定義視圖添加樣式屬性的配置,今天武林技術頻道小編在這里詳解Android 之View時使用TypedArray配置樣式的屬性介紹,一起來看看吧!
Android 之View時使用TypedArray配置樣式的屬性介紹
先上效果圖:

點擊以后為

再貼代碼:
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/包名
武林技術頻道小編是Android 之View時使用TypedArray配置樣式的屬性介紹,等自己學習到一定的知識之后再去進修,其實這個行業就業前景真的很不錯,值得您選擇哦!
新聞熱點
疑難解答