Android教你一步一步实现简单重用自定义控件
16lz
2021-01-23
老规矩,先看图,没图说个JB
像这种的布局都是固定的样式,只是图片和文字改变,要是按照正常的写布局文件,要写好长,重用性很差。
1.先创建一个布局文件
<?xml version="1.0" encoding="utf-8"?><RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"> <ImageView android:id="@+id/iv_left" android:layout_height="52dp" android:layout_width="52dp" android:src="@drawable/ic_smart_ask" android:layout_centerVertical="true" /> <LinearLayout android:id="@+id/ll_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:gravity="center_vertical" android:orientation="vertical" android:layout_toRightOf="@id/iv_left" android:layout_marginLeft="@dimen/text_layout_18_dp" > <TextView android:id="@+id/tv_top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="智能问诊" android:textColor="#ff333333" android:textSize="16sp" /> <TextView android:id="@+id/tv_bottom" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="快速得到解决方案,避免盲目就医" android:textColor="@color/tv999" android:textSize="13sp" android:layout_marginTop="@dimen/text_layout_10_dp" /> </LinearLayout> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_right" android:layout_centerVertical="true" android:layout_alignParentRight="true" /> </RelativeLayout>
2.自定义控件类
/** * 问诊自定义View * Created by Administrator on 2018/12/15. */public class AskLayoutNormalView extends RelativeLayout { ImageView leftImage; TextView topText; TextView bottomText; public AskLayoutNormalView(Context context) { super(context); } public AskLayoutNormalView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.layout_ask_text_normal,this,true); leftImage = findViewById(R.id.iv_left); topText = findViewById(R.id.tv_top); bottomText = findViewById(R.id.tv_bottom); TypedArray attributes = context.obtainStyledAttributes(attrs,R.styleable.AskLayoutNormalView); if(attributes != null){ int leftImg = attributes.getResourceId(R.styleable.AskLayoutNormalView_leftDrawable,-1); String topString = attributes.getString(R.styleable.AskLayoutNormalView_topText); String bottomString = attributes.getString(R.styleable.AskLayoutNormalView_bottomText); if(leftImg != -1){ leftImage.setImageResource(leftImg); } topText.setText(topString); bottomText.setText(bottomString); } } public AskLayoutNormalView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }}
3.在attrs中定义标签
<!--问诊自定义View--> <declare-styleable name="AskLayoutNormalView"> <attr name="leftDrawable" format="reference|integer"></attr> <attr name="topText" format="string"></attr> <attr name="bottomText" format="string"></attr> </declare-styleable>
4.在布局文件中使用
<com.jkgl.view.AskLayoutNormalView android:layout_width="match_parent" android:layout_height="wrap_content" app:leftDrawable="@drawable/ic_smart_ask" app:topText="智能问诊" app:bottomText="快速得到解决方案,避免盲目就医" android:layout_marginLeft="@dimen/text_layout_15_dp" android:layout_marginRight="@dimen/text_layout_15_dp" android:layout_marginTop="@dimen/text_layout_10_dp" android:paddingLeft="@dimen/text_layout_15_dp" android:paddingRight="@dimen/text_layout_15_dp" android:paddingTop="@dimen/text_layout_30_dp" android:paddingBottom="@dimen/text_layout_30_dp" android:background="@color/white" > </com.jkgl.view.AskLayoutNormalView>
其中
app:leftDrawable="@drawable/ic_smart_ask"app:topText="智能问诊"app:bottomText="快速得到解决方案,避免盲目就医"
这三个标签就是填写数据的。
结束…
是不是很简单,还很实用~
更多相关文章
- Android打开或者关闭软键盘的解决方案
- ionic3文件目录介绍
- android从未安装的apk文件里获取信息(包信息,资源信息)
- 提高开发效率-使用Android Studio Template快速生成模板文件
- Android API开发之OpenGL开发之Android OpenGL显示STL模型文件
- android页面布局总结
- Android Mediaplayer error (1, -2147483648) Error (-38,0)解决
- android studio R文件找不到