自定义view-使用xml控制界面的呈现
2017-02-26 22:15
288 查看
1.首先我们需要在res目录下的values目录下创建attrs.xml资源文件文件并在里面配置我们需要添加的新功能
代码如下:
2.之后就使我们的自定义控件了代码如下
3.最后在xml中调用并且设置相关属性
代码如下
代码如下:
<re 4000 sources> <declare-styleable name="tvAttrs"> <attr name="leftText" format="string"/> <attr name="rightText" format="string"/> </declare-styleable> </resources>
2.之后就使我们的自定义控件了代码如下
public class MyTextView extends android.support.v7.widget.AppCompatTextView { String titles=""; public MyTextView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.tvAttrs); String leftText=ta.getString(R.styleable.tvAttrs_leftText); String rightText=ta.getString(R.styleable.tvAttrs_rightText); ta.recycle(); titles=leftText+rightText; setText(titles); } }
3.最后在xml中调用并且设置相关属性
代码如下
<com.example.kangjiahang.testpicasso.MyTextView android:id="@+id/tv_main" android:layout_width="wrap_content" android:layout_height="wrap_content" app:leftText="adc" app:rightText=" dde" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
相关文章推荐
- 【Android UI设计与开发】第02期:引导界面(二)使用ViewPager2、其中小点的图片用一个selector来控制颜色,point.xml:现欢迎引导页面
- Android自定义View及自定义控件属性declare-styleable:自定义控件的属性(attr.xml,TypedArray)的使用
- 如何在xml中使用自定义的ViewGroup组件
- 自定义View以及View的属性的使用(重点,xml中定义属性的值),包括Bitmap存为jpg样式(可以模仿修改照片不喜欢的地方)
- 使用原生自定义View,setState刷新界面后UI不能正常显示
- Android界面设计的4种方式之三——使用XML和JAVA代码混合控制UI界面
- 自定义view时使用xml传递参数-设定TextView中图片大小
- Android学习个人笔记1-Layout界面布局-xml,java混合模式-自定义view
- Android自定义View中的自定义属性(attrs.xml,TypedArray的使用)
- 自定义View,又一种进度条的呈现,CircleProgressView使用解析
- 自定义view在XML中使用
- 使用XIB自定义一个UIView,然后将这个view添加到controller的view 上(相当于所有界面都通过xib来实现)
- 2011.8.17---2011.8.18 (查询bug---contactWidget中使用自定义的view在launcher中显示不出来,报解析xml的错误)
- 使用XIB自定义一个UIView,然后将这个view添加到controller的view 上(相当于所有界面都通过xib来实现)
- android应用面试宝典(下);用自定义组件控制循环组件使用bug还添加标志显示答案标志位;通过包名读取配置文件AndroidManifest.xml
- android 自定义View并在xml中控制
- 使用xml 控制imageview的缩放
- 使用UITableView的自定义UITableViewCell构建聊天界面
- ios-day09-02(QQ好友列表。自定义UITableViewHeaderFooterView、如何控制UITableView每一组的展开和闭合、代理的使用)
- 自定义的LinearLayout 使用报错 android.view.InflateException: Binary XML file line #3: Error inflating class