自定义RelativeLayout view实现布局。
来源:互联网 发布:自己写一个linux系统 编辑:程序博客网 时间:2024/05/09 07:21
在开发中,一个界面或者几个界面通常用到的基本UI都是相似的。这时候如果布局文件的话 往往会涉及到多个布局文件。我们更倾向于自定义的view。
UI:
public class WFFE_FilePickerView2 extends RelativeLayout implements WFIFormElementActivityResult{ private TextView mTxtTitle; private ImageView mImgIcon; private TextView mTxtValue; private ImageView mImgArrow; private WFFE_FilePicker2 element; private static final int ID_TITLE_LABEL = 0x1001; private static final int ID_RIGHT_ARROW = 0x1002; private static final int ID_MIDDEL_IMG = 0x1003; private static final int ID_VALUE_TXT = 0x1004; public WFFE_FilePickerView2(Context context, WFFE_FilePicker2 filePicker) { super(context); this.element = filePicker; this.initAttributes(); } public WFFE_FilePickerView2(Context context, AttributeSet attrs) { super(context, attrs); } @SuppressWarnings("ResourceType") private void initAttributes() { if (null == getContext() || null == element) { return; } final int imgWidth = GlobalUtil.getPixelsByDp(getContext(), 20.0F); // // initializes this ViewGroup and all child views // LinearLayout.LayoutParams viewGroupLayoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); ViewUtils.setMarginParams( this.getContext(), viewGroupLayoutParams, true, WFIFormElement.MARGIN_12DP, WFIFormElement.MARGIN_5DP, WFIFormElement.MARGIN_12DP, WFIFormElement.MARGIN_5DP); this.setLayoutParams(viewGroupLayoutParams); this.setGravity(Gravity.CENTER_VERTICAL); mImgArrow = new ImageView(getContext()); mImgArrow.setId(ID_RIGHT_ARROW); mImgArrow.setImageResource(R.drawable.icon_right_arrow); LayoutParams arrowParams = new LayoutParams(imgWidth, imgWidth); arrowParams.addRule(ALIGN_PARENT_RIGHT); arrowParams.addRule(CENTER_VERTICAL); ViewUtils.setMarginParams(getContext(), arrowParams, true, WFIFormElement.MARGIN_5DP, 0, 0, 0); mTxtTitle = new TextView(getContext()); mTxtTitle.setId(ID_TITLE_LABEL); mTxtTitle.setTextSize(WFIFormElement.TEXT_SIZE_15SP); LayoutParams titleParam = new LayoutParams(LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); titleParam.addRule(ALIGN_PARENT_LEFT); CharSequence titleText = GlobalUtil.appendHeadAsterisk(getContext(), element.isRequired(), element.label, Constants.GREEN); mTxtTitle.setText(titleText); mTxtValue = new TextView(getContext()); mTxtValue.setId(ID_VALUE_TXT); LayoutParams valueParam = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); valueParam.addRule(LEFT_OF, mImgArrow.getId()); mTxtValue.setGravity(Gravity.RIGHT); mTxtValue.setVisibility(GONE); mImgIcon = new ImageView(getContext()); mImgIcon.setId(ID_MIDDEL_IMG); LayoutParams iconParam = new LayoutParams(imgWidth, imgWidth); iconParam.addRule(LEFT_OF, mTxtValue.getId()); mImgIcon.setImageResource(R.drawable.icon_right_arrow); ViewUtils.setMarginParams(getContext(), iconParam, true, WFIFormElement.MARGIN_5DP, 0, WFIFormElement.MARGIN_5DP, 0); mImgIcon.setVisibility(GONE); View lineView = new View(getContext()); lineView.setBackgroundColor(Constants.GREEN); LayoutParams lineParam = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 1); lineParam.setMargins(WFIFormElement.MARGIN_5DP, WFIFormElement.MARGIN_5DP, WFIFormElement.MARGIN_5DP, WFIFormElement.MARGIN_5DP); lineParam.addRule(BELOW, mTxtTitle.getId()); this.addView(mTxtTitle, titleParam); this.addView(mImgIcon, iconParam); this.addView(mTxtValue, valueParam); this.addView(mImgArrow, arrowParams); this.addView(lineView, lineParam); setupListener(); } private void setupListener() { if (null != mImgArrow && mImgArrow.getVisibility() == VISIBLE) { mImgArrow.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { intentToFilePickerPage(); } }); } } private void intentToFilePickerPage() { Intent intent = new Intent(getContext(), BM_ContentChooserActivity.class); intent.putExtra(BM_ContentChooserFragment.FILE_TYPE, BM_ContentChooserFragment.TypeFlag.FORM); intent.putExtra(BM_ContentChooserFragment.CONTENT_SELECTED_ONE, true); ((Activity) getContext()).startActivityForResult(intent, 1001); } @Override public void handleActivityResult(int requestCode, int resultCode, Intent data) { if (element.showIcon) { this.mImgIcon.setVisibility(VISIBLE); } FileRecord fileRecord = (FileRecord) data.getSerializableExtra(BM_ContentChooserFragment.SELECTED_FILERECORD); if (element.showValue) { this.mTxtValue.setVisibility(VISIBLE); this.mTxtValue.setText(fileRecord.name); } }}开发中我们的习惯是(至少我的习惯是):定义一个view 比如说 label img text img。习惯是从左往右按顺序定义。这样的话 有时候很多UI不能实现。
1 0
- 自定义RelativeLayout view实现布局。
- 自定义view继承RelativeLayout
- 自定义RelativeLayout布局
- RelativeLayout实现百分比布局
- 代码实现 RelativeLayout布局
- relativelayout实现梅花布局
- 自定义布局RelativeLayout 画网格线
- 自定义布局RelativeLayout 画网格线
- 自定义actionBar --RelativeLayout实现
- android 自定义view实现流式布局
- 自定义ViewGroup实现view的自由布局
- Android自定义View实现流式布局
- 自定义view实现流式布局
- RelativeLayout代码实现相对布局
- android-RelativeLayout实现水平布局
- 动态实现RelativeLayout,LinearLayout布局
- Android 自定义布局控件-圆形RelativeLayout
- 自定义实现简单方形RelativeLayout
- Java进阶(十八)Java实现定时器(Timer)
- jackson异常处理
- udhcpc命令
- Android 使用 Scheme 启动淘宝,天猫等其他APP
- 简单的一个在线聊天室
- 自定义RelativeLayout view实现布局。
- Glide使用详解(1)
- mysql性能监控软件 慢日志分析利器
- 生成唯一标识符
- linux 下 删除了两个ORACLE 的两个很大的数据表文件,但是查系统剩余空间未减少
- strcmp函数的使用
- Xcode8.3制作动态及静态framework 笔记
- Context namespace element 'component-scan' and its parser class [org.springframework.context.annotat
- Android 百度音乐盒