/** * Created by jiangchen 334967198@qq.com on 2017/4/20- 11:02. * 说明:自定义View */ public class LeftRightTextView extends LinearLayout { private LayoutInflater layoutInflater; private View view; private TextView leftTv; private TextView rightTv; private String textLeft, textRight; private int layoutType; private int leftWidth; public LeftRightTextView(Context context, AttributeSet attrs) { super(context, attrs); layoutInflater = LayoutInflater.from(context); /** *2017/4/20 11:13 *Created by jiangchen *作用:通过XML布局里面的属性来给这些变量设值 */ if (attrs != null) { //R.styleable.LeftRightTextView----在attrs里面写的declare-styleable TypedArray是获取XML配置参数的数组集 final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.LeftRightTextView); //获取值,默认值!!!!LeftRightTextView_left_width!!!!!! 命名格式 leftWidth = a.getInteger(R.styleable.LeftRightTextView_left_width, 100); layoutType = a.getInteger(R.styleable.LeftRightTextView_style_type, 1); textLeft = a.getString(R.styleable.LeftRightTextView_text_left); textRight = a.getString(R.styleable.LeftRightTextView_text_right); //释放内存,防止泄露 a.recycle(); } //给自定义的view设置布局 if (layoutType == 1) { //如果type值为1,则用mytextview_type_one这个布局 view = layoutInflater.inflate(R.layout.mytextview_type_one, this); } else { //如果type值为2,则用mytextview_type_two这个布局 view = layoutInflater.inflate(R.layout.mytextview_type_two, this); } //获取左边和右边的textview实例 leftTv = (TextView) view.findViewById(R.id.left_tv); rightTv = (TextView) view.findViewById(R.id.right_tv); //设值 leftTv.setText(textLeft); rightTv.setText(textRight); leftTv.setWidth(leftWidth); } /** *2017/4/20 11:49 *Created by jiangchen *作用:公共方法,设置左边textview的文字 */ public void setLeftText(String str){ leftTv.setText(str); } /** *2017/4/20 11:49 *Created by jiangchen *作用:公共方法,设置左边textview的文字 */ public void setRightText(String str){ rightTv.setText(str); } /** *2017/4/20 11:49 *Created by jiangchen *作用:公共方法,设置左边textview的文字 */ public void setLeftWidth(int w){ leftTv.setWidth(w); }}
//新建value 下的attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="LeftRightTextView"> <!--变量名,格式--> <attr name="text_left" format="string"/> <attr name="text_right" format="string"/> <attr name="left_width" format="integer"/> <attr name="style_type" format="integer"/> </declare-styleable> </resources>
自定义控件的布局,type1
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <TextView android:id="@+id/left_tv" android:layout_width="150dp" android:layout_height="match_parent" android:gravity="center_vertical"/> <TextView android:id="@+id/right_tv" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center_vertical"/> </LinearLayout>
自定义控件的布局,type2
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ImageView android:layout_width="20dp" android:layout_height="match_parent" android:src="@mipmap/ic_launcher"/> <TextView android:id="@+id/left_tv" android:layout_width="150dp" android:layout_height="match_parent" android:gravity="center_vertical"/> <TextView android:id="@+id/right_tv" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center_vertical"/> </LinearLayout>
public class MainActivity extends Activity { private LeftRightTextView myTv; //自定义控件 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myTv = (LeftRightTextView) findViewById(R.id.my_tv); //获取实例 myTv.setLeftText("我最帅"); //左边文字 myTv.setRightText("肯定我最帅"); //右边文字 myTv.setLeftWidth(200); //左边宽度 } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><!--自定义属性所用--> <!--xmlns:mypersonvalues="http://schemas.android.com/apk/res-auto"这行代码必要,名字自己随便取--> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:mypersonvalues="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.vkeline.mypersonview.MainActivity"> <!--自定义属性所用style_type="2"--> <com.vkeline.mypersonview.LeftRightTextView android:layout_width="match_parent" android:layout_height="40dp" mypersonvalues:left_width="300" mypersonvalues:style_type="2" mypersonvalues:text_left="哈哈" mypersonvalues:text_right="哈哈"> </com.vkeline.mypersonview.LeftRightTextView> <!--自定义属性所用style_type="1"--> <com.vkeline.mypersonview.LeftRightTextView android:id="@+id/my_tv" android:layout_width="match_parent" android:layout_height="40dp" mypersonvalues:style_type="1"> </com.vkeline.mypersonview.LeftRightTextView> </LinearLayout>
android 自定义控件
最新推荐文章于 2023-10-20 13:54:18 发布