1: 自定义TextView
package com.welleplus.yfinspection.ui;
import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.TintTypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.welleplus.yfinspection.R;
/**
* description :
* Author : Vily
* Date : 2018/09/11
* Time : 14:55
*/
public class MyTextView extends LinearLayout {
private Context mContext;
private LayoutInflater mInflater;
private View mView;
private TextView mTv_title;
private ImageView mIv_direction;
public MyTextView(Context context) {
this(context,null);
mContext=context;
mInflater=LayoutInflater.from(context);
}
public MyTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
@SuppressLint("RestrictedApi")
public MyTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
if (attrs != null) {
final TintTypedArray a = TintTypedArray.obtainStyledAttributes(getContext(), attrs,
R.styleable.MyTextView, defStyleAttr, 0);
// 从属性中获取 rightButtonTitle
String title = a.getString(R.styleable.MyTextView_text_title);
if (title != null) {
setTitle(title);
}
// 资源回收
a.recycle();
}
}
public void setTitle(String title) {
if(!TextUtils.isEmpty(title)){
mTv_title.setText(title);
}
}
private void init() {
if(mView==null){
mInflater=LayoutInflater.from(getContext());
mView = mInflater.inflate(R.layout.textview_ui, null);
mTv_title = mView.findViewById(R.id.tv_title);
mIv_direction = mView.findViewById(R.id.iv_direction);
//然后使用LayoutParams把控件添加到子view中
LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
addView(mView,params);
}
}
public void setUp(){
mIv_direction.setImageResource(R.drawable.top);
}
public void setDown(){
mIv_direction.setImageResource(R.drawable.down);
}
}
2: attrs
<declare-styleable name="MyTextView">
<attr name="text_title" format="string"/>
</declare-styleable>
3: XML 使用
<com.welleplus.yfinspection.ui.MyTextView
android:id="@+id/tv_area"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
app:text_title="区域"
android:tag="area"
/>
4: 代码中点击弹框 自定义弹框 ------->
List<String> list2 = new ArrayList<>();
list2.add("全部");
list2.add("核心设备");
list2.add("主要设备");
list2.add("辅助设备");
if (mPopuWindow == null) {
mPopuWindow = new MyPopuWindow(mTvDevice, getActivity(), list2);
} else {
mPopuWindow.setData(mTvDevice, list2);
}
mTvDevice.setUp();
popuClick(mTvDevice);
private void popuClick(final MyTextView myTextView) {
if (mPopuWindow != null) {
mPopuWindow.setOnItemCLickListener(new MyPopuWindow.OnItemCLickListener() {
@Override
public void onItemCLick(String item) {
mCurrPage = 0;
mCacheAdapter.setNewData(null);
myTextView.setTitle(item);
switch (item) {
case "全部":
String trim = myTextView.getTag().toString().trim();
if(!TextUtils.isEmpty(trim)){
switch (trim){
case "area" :
myTextView.setTitle("区域");
mMap.put("area", "");
break;
case "type" :
myTextView.setTitle("设备类型");
mMap.put("etype", "");
break;
default :
break;
}
}
break;
case "核心设备":
mMap.put("etype", "核心设备");
break;
case "主要设备":
mMap.put("etype", "主要设备");
break;
case "辅助设备":
mMap.put("etype", "辅助设备");
break;
default:
break;
}
mMap.put("page", mCurrPage + "");
requestData(mMap);
}
});
mPopuWindow.setOnPopuDismissListener(new MyPopuWindow.OnPopuDismissListener() {
@Override
public void onPopuDismiss() {
myTextView.setDown();
}
});
}
}