自定义组件(三)
来源:互联网 发布:追风打印软件 编辑:程序博客网 时间:2024/05/21 20:46
本节课的目标:创建布局的自定义属性
1.在values下创建attr
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="CompoundView"> <attr name="max" format="integer"/> <attr name="progress" format="integer" /> </declare-styleable></resources>
CompoundView就是我们styleable 的name
max,progress是我们定义的属性
2.在布局CompoundView的构造方法中添加TypedArray对象
绑定自定义属性
public CompoundView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.CompoundView); max= ta.getInt(R.styleable.CompoundView_max,100); progress= ta.getInt(R.styleable.CompoundView_progress, 0); ta.recycle(); initView(context); }
在引用自定义布局的xml引入自定义属性
xmlns:app="http://schemas.android.com/apk/res-auto"
可以设置属性的初始值
<com.axnet.viewapp.CompoundView android:id="@+id/main_in" android:layout_height="wrap_content" android:layout_width="match_parent" layout="@layout/main_item" app:max="10" app:progress="2" />
全部代码
MainActivity
public class MainActivity extends AppCompatActivity { Button main_btn,main_item_btn; CompoundView myView; int i; boolean b=true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initClick(); } private void initClick() { //下载按钮 main_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { b=true; new Thread(new Runnable() { @Override public void run() { while (b){ try { Thread.sleep(1000); runOnUiThread(new Runnable() { @Override public void run() { myView.setProgress(i++); } }); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } });// myView.setMax(50); myView.setListener(new View.OnClickListener() { @Override public void onClick(View v) { b=false; i=0; myView.setProgress(i); } }); } private void initView() { main_btn=(Button)findViewById(R.id.main_btn); myView= (CompoundView) findViewById(R.id.main_in); }}
MainActivity布局代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <!--自定义命名空间--> <!--xmlns:app="http://schemas.android.com/apk/res-auto"--> <Button android:id="@+id/main_btn" android:text="下载" android:layout_height="wrap_content" android:layout_width="match_parent" /> <com.axnet.viewapp.CompoundView android:id="@+id/main_in" android:layout_height="wrap_content" android:layout_width="match_parent" layout="@layout/main_item" app:max="10" app:progress="2" /></LinearLayout>
CompoundView 代码
public class CompoundView extends LinearLayout{ private Button btn; private ProgressBar mProgressBar; private int max;//最大进度 private int progress;//当前进度 private OnClickListener listener; public CompoundView(Context context) { super(context); initView(context); } public CompoundView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.CompoundView); max= ta.getInt(R.styleable.CompoundView_max,100); progress= ta.getInt(R.styleable.CompoundView_progress, 0); ta.recycle(); initView(context); } public CompoundView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context); } @Override protected void onFinishInflate() { super.onFinishInflate(); //设置进度条默认最大值 mProgressBar.setMax(max); mProgressBar.setProgress(progress); } //提供给使用者设置butter监听的 public void setListener(OnClickListener listener) { this.listener = listener; //为butter设置监听 btn.setOnClickListener(listener); } public void setMax(int max) { this.max = max; mProgressBar.setMax(max); } public void setProgress(int progress) { if(progress>max){ this.progress=max; }else { this.progress = progress; } mProgressBar.setProgress(progress); } private void initView(Context mContext ){ LayoutInflater inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); //将compound_view导入LinearLayout中 inflater.inflate(R.layout.compound_view,this); btn= (Button) this.findViewById(R.id.view_item_btn); mProgressBar=(ProgressBar)this.findViewById(R.id.view_pbar); }}
compound_view 布局代码
<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="horizontal" android:gravity="center" android:padding="20dp" android:layout_height="match_parent"> <ProgressBar android:layout_width="5dp" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/view_pbar" style="?android:attr/progressBarStyleHorizontal" /> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/view_item_btn" android:layout_weight="1" android:text="取消下载" /></merge>
在values下创建啊attr
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="CompoundView"> <attr name="max" format="integer"/> <attr name="progress" format="integer" /> </declare-styleable></resources>
阅读全文
0 0
- 自定义组件(三)
- 自定义组件研究<三>
- ASP.NET自定义控件组件开发(三)
- 安卓四大组件复习----自定义广播(三)
- Vue.js的组件(三)父组件与子组件的数据联系 之 子传父 之 自定义事件
- 自定义组件实现三种进度条
- Unity自定义UI组件(三)饼图篇
- Unity自定义UI组件(三)饼图篇
- 自定义组件(第一)
- 自定义组件(二)
- 自定义组件(一)
- 自定义组件(1)
- 自定义组件(二)
- 自定义组件(四)
- 组件生命周期(三)
- 组件生命周期(三)
- Android组件(三)
- 组件生命周期(三)
- [PAT乙级]1063. 计算谱半径(20)
- Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
- 锚点滚动效果
- Layer 弹出层在ie8下 错乱的解决办法 亲测完美解决
- js只能输入数字和小数点的文本框
- 自定义组件(三)
- 【学习】kudu、Impala、交互式查询
- MVC 使用cshtml的一些基础知识-和相关整理
- 使用AppBarLayout+CollapsingToolbarLayout实现自定义工具栏折叠效果
- MUI框架(2)
- JavaScript对象(2)——创建对象
- SpringMVC整合Hibernate Validator验证方法与js校验
- 文章标题
- FCC-Basic Algorithm Scripting