android自定义控件中文乱码,Android笔记--自定义View之组合控件
Android-自定義View
分享是最好的記憶--
如需轉發請注明出處
[強調]:共同學習 共同進步 不喜勿噴
內容簡介
前言
實現
總結
1. 前言
這次更新有2個目的
1. 復用控件,而不是每次都寫冗余代碼
2. 好久沒有更新了(?? . ??)
在Android開發當中自定義View是一項不可或缺的技能,也是我們大部分小白以及小小白的軟肋。
自定義View其實也不是很難,因為我們不需要攻克什么技術難關,只需要坐在巨人的肩膀上,抱緊巨人的腦袋就行(?>ω
自定義View分兩大類:
一 : 組合 : 也就是在現有的控件基礎上通過組合各種不同功能的控件來組合出功能更強大全民的新控件。
二 : 完全自定義:通過繼承View或者ViewGroup來自定義全新的View。??今天討論的自定義View是第一種方式,即組合方式。如果想了解完全自定義或者更多自定義方面的知識,推薦看扔物線大神以及張鴻翔大神的文章。
2. 實現
1. 確定需求
功能列表中的Item是我們經常用到的控件之一。它也許是這樣的:
item1.png
或者是這樣的:
item2.png
再或者是這樣的:
item3.png
為了滿足這些情況,我們抽象出功能最豐富的Item;確定了需求之后開始編碼。
2. 編碼
1. 自定義屬性
首先確定我們需要動態設置的屬性;包括 :1-左邊圖片,2-3-左邊圖片的寬高,4-大標題,5-大標題文字大小,6-大標題文字顏色,7-副標題,8-副標題文字大小,9-副標題文字顏色,10-右邊圖片,11-12-右邊圖片寬高等12個屬性,當然不止這些,這個可以根據自己的需求豐富,比如 Item 下面是否顯示陰影線條,右邊圖片是否顯示等;
那么在../res/attrs.xml中定義自定義屬性;
這里有必要解釋一下屬性定義中的format
name
format
描述
資源
reference
通過id來指定資源
顏色
color
設置顏色
尺寸
dimension
長寬,字體大小等尺寸
字符串
String
設置文本
布爾值
boolean
通過布爾值設置
浮點值
float
設置浮點值
百分數
fraction
設置百分數值
枚舉值
enum
設置枚舉值
位或運算
flag
位或運算
整型值
integer
設置整型
屬性定義時可以指定多種類型值
2.抽象組建
這里說的抽象組建是把該Item可能有的所有功能都寫出來;
在../res/layout/item.xml中寫出包含組建所有功能的樣式;
android:layout_width="match_parent"
android:layout_height="wrap_content">
style="@style/UILibraryListItemBase"
android:id="@+id/leftImg"
android:layout_alignParentLeft="true"
android:padding="8dp"
/>
style="@style/UILibraryListItemBase"
android:id="@+id/title"
android:layout_toRightOf="@+id/leftImg"
/>
style="@style/UILibraryListItemBase"
android:id="@+id/subTitle"
android:layout_toLeftOf="@+id/rightImg"
android:layout_toRightOf="@+id/title"
android:gravity="right"
/>
style="@style/UILibraryListItemBase"
android:id="@+id/rightImg"
android:layout_alignParentRight="true"
android:padding="8dp"
/>
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_alignBottom="@+id/leftImg"
/>
3.實現功能
選擇一個適當的ViewGroup來繼承,實現Item;
我的樣式中rootView是個RelativeLayout,因此我繼承了RelativeLayout。具體實現如下:
package com.uilib.ui.listitem;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.uilibrary.R;
import com.uilib.ui.util.FindView;
/**
* Created by Elyar.Anwar on 2017/8/10.
*/
public class UILibraryListItem extends RelativeLayout {
/**
* 選項控件的左邊圖片
*/
private ImageView leftImageView;
/**
* 選項控件的右邊圖片
*/
private ImageView rightImageView;
/**
* 選項控件的標題
*/
private TextView titleView;
/**
* 選項控件的副標題
*/
private TextView subTitleView;
/**
* 左邊圖片資源
*/
private Drawable leftImage;
/**
* 左邊圖片資源寬度
*/
private int leftImageWidth;
/**
* 左邊圖片資源高度
*/
private int leftImageHeight;
/**
* 右邊圖片資源
*/
private Drawable rightImage;
/**
* 右邊圖片資源寬度
*/
private int rightImageWidth;
/**
* 右邊圖片資源高度
*/
private int rightImageHeight;
/**
* 標題文本
*/
private String title;
/**
* 標題文本顏色
*/
private int titleTextColor;
/**
*標題文本字體大小
*/
private float titleTextSize;
/**
*副標題文本
*/
private String subTitle;
/**
*副標題文本顏色
*/
private int subTitleTextColor;
/**
*副標題文本字體大小
*/
private float subTitleTextSize;
/**
*上下文
*/
private Context mContext;
/**
* 屬性結合
*/
private AttributeSet attrs;
public UILibraryListItem(Context context) {
this(context,null);
}
public UILibraryListItem(Context context, AttributeSet attrs) {
this(context, attrs,0);
this.mContext = context;
this.attrs = attrs;
init();
}
public UILibraryListItem(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.mContext = context;
this.attrs = attrs;
init();
}
/**
* 初始化視圖
*/
private void init() {
View.inflate(mContext, R.layout.listitem, this);
findView();//綁定視圖
loadParams();//加載屬性值
initView();//為視圖賦值
}
/**
* 為控件賦值
*/
private void initView() {
if(leftImage!=null){
leftImageView.setImageDrawable(leftImage);
}
leftImageView.getLayoutParams().width = leftImageWidth;
leftImageView.getLayoutParams().height = leftImageHeight;
if(rightImage!=null){
rightImageView.setImageDrawable(rightImage);
}
rightImageView.getLayoutParams().width = rightImageWidth;
rightImageView.getLayoutParams().height = rightImageHeight;
if (!TextUtils.isEmpty(title)){
titleView.setText(title);
titleView.setTextColor(titleTextColor);
titleView.setTextSize(titleTextSize);
}
if (!TextUtils.isEmpty(subTitle)){
subTitleView.setText(subTitle);
subTitleView.setTextColor(subTitleTextColor);
subTitleView.setTextSize(subTitleTextSize);
}
}
/**
* 初始化控件的屬性
*/
private void loadParams() {
TypedArray typedArray = mContext.obtainStyledAttributes(attrs,R.styleable.UILibraryListItem);
leftImage = typedArray.getDrawable(R.styleable.UILibraryListItem_leftImg);
leftImageHeight = typedArray.getDimensionPixelSize(R.styleable.UILibraryListItem_leftImg_height,128);
leftImageWidth = typedArray.getDimensionPixelSize(R.styleable.UILibraryListItem_leftImg_width,128);
rightImage = typedArray.getDrawable(R.styleable.UILibraryListItem_rightImg);
rightImageHeight = typedArray.getDimensionPixelSize(R.styleable.UILibraryListItem_rightImg_height,128);
rightImageWidth = typedArray.getDimensionPixelSize(R.styleable.UILibraryListItem_rightImg_width,128);
title = typedArray.getString(R.styleable.UILibraryListItem_title);
titleTextColor = typedArray.getColor(R.styleable.UILibraryListItem_title_color,Color.BLACK);
titleTextSize = typedArray.getDimensionPixelSize(R.styleable.UILibraryListItem_title_size,20);
subTitle = typedArray.getString(R.styleable.UILibraryListItem_subTitle);
subTitleTextColor = typedArray.getColor(R.styleable.UILibraryListItem_subTitle_color,Color.GRAY);
subTitleTextSize = typedArray.getDimensionPixelSize(R.styleable.UILibraryListItem_subTitle_size,14);
typedArray.recycle();
}
/**
* 關聯控件
*/
private void findView() {
leftImageView = FindView.findView(this,R.id.leftImg);
rightImageView = FindView.findView(this,R.id.rightImg);
titleView = FindView.findView(this,R.id.title);
subTitleView = FindView.findView(this,R.id.subTitle);
}
}
4.引用并預覽
引用時為了看出明顯效果我定義了2個Item;
xmlns:uilib="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:id="@+id/it"
android:layout_width="match_parent"
android:layout_height="wrap_content"
uilib:leftImg="@drawable/icon_a"
uilib:rightImg="@drawable/icon_b"
uilib:subTitle="SubTitle"
uilib:title="Title" />
android:id="@+id/it2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
uilib:leftImg="@drawable/test_icon_allow"
uilib:subTitle="SubTitle"
uilib:subTitle_color="@color/red"
uilib:title="Title" />
于是完成結果如下:
2017-08-11_004205.png
可以根據自己的實際情況靈活改變樣式,且僅僅在第四步引用處做出更改即可;
總結
自定義View之組合;
通過組合來自定義View能滿足我們大部分的需求,且相對容易。
總結其過程:
1.自定義屬性
2.抽象出樣式
3.編碼實現類
4.引用和預覽
我是ElyarAnwar,在技術的道路上摸爬滾打;
熱愛生活,熱愛技術;如果喜歡記得點贊;
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的android自定义控件中文乱码,Android笔记--自定义View之组合控件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 原始定位,安卓原生定位
- 下一篇: android内存不足,Android