纯代码RelativeLayout实现右上角布局
来源:互联网 发布:类似yolo的软件 编辑:程序博客网 时间:2024/05/17 21:45
需求:纯代码RelativeLayout实现功能,一个popwindow放置一张图片,一个叉叉按钮放置到图片右上角;
效果图
思路:两个ImageView内置图片,添加到popwindow,设置叉叉位置到右上角;
简单说说几个坑
1,setBackgroundDrawable和setImageBitmap
android.view.View.setBackgroundDrawable这个方法会拉升图片
2,ImageView和ImageButton
ImageButton有边框
3,实现思路:直接设置叉叉在图片右对齐,然后设置图片的setPadding,往左下的距离分别为叉叉宽高的一半(图片右上角和叉叉圆心重合,这样就比较好看)
//icon是图片int iconID=1234;iconImageView.setId(iconID);exitParams.addRule(RelativeLayout.ALIGN_RIGHT,iconID);exitParams.addRule(RelativeLayout.ALIGN_TOP,iconID);
4,因为采用了popwindow和RelativeLayout,如果设置叉叉相对popwindow右上角对齐的话,图片和叉叉之间会有空白,叉叉和图片相隔很远;
5,怎么避免那些坑?
设置popwindow的底色为blue,设置图片的底色red,设置叉叉的底色green,然后尝试上面的不同方法,设置不同的位置,看到效果就知道问题在哪里。
6,总结:布局问题,特别是相对布局问题,设置底色看效果,这是最显而易见的方式。
7,关键代码
public final static String TAG = FullWindow.class.getSimpleName(); private XImageView iconImageView; public FullWindow(final Context context) { super(context); String iconURL = "http://avatar.csdn.net/8/E/A/1_codehxy.jpg"; FullWindow.this.setClickable(true); FullWindow.this.setFocusable(true); FullWindow.this.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); final ImageView exitButton = new ImageView(context); //创建广告 iconImageView = new XImageView(context,new ExitListener(){ @Override public void show() { exitButton.setImageBitmap(getBitmap("i/delete_64.png", context));// } }); exitButton.setBackgroundColor(Color.TRANSPARENT);// exitButton.setBackgroundColor(Color.BLUE);//调试用的 RelativeLayout.LayoutParams exitParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); int iconID=1234; iconImageView.setId(iconID); exitParams.addRule(RelativeLayout.ALIGN_RIGHT,iconID); exitParams.addRule(RelativeLayout.ALIGN_TOP,iconID); //添加广告到界面上 iconImageView.loadImage(context,iconURL); FullWindow.this.addView(iconImageView); //将退出按钮添加到界面上 FullWindow.this.addView(exitButton, exitParams); //设置退出按钮点击监听事件 exitButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { PopWindowManager.removeFullWindow(context); } catch (Exception e) { e.printStackTrace(); } } }); //设置广告点击监听事件 iconImageView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { PopWindowManager.removeFullWindow(context); } catch (Exception e) { e.printStackTrace(); } } }); } } public interface ExitListener { void show(); } public static Bitmap getBitmap(String imgPath, Context context) {//注意这个context,图片需要放到宿主的src路径下,否则不能加载到 try { InputStream inputStream = context.getClassLoader().getResourceAsStream(imgPath); Bitmap bitmap=BitmapFactory.decodeStream(inputStream); LogUtil.d(TAG, "getBitmap inputStream:"+ inputStream); LogUtil.d(TAG, "getBitmap bitmap:"+ bitmap); return bitmap; } catch (Exception e) { LogUtil.d(TAG, "getBitmap Exception:"+ e); e.printStackTrace(); } return null; }
0 0
- 纯代码RelativeLayout实现右上角布局
- 代码实现 RelativeLayout布局
- RelativeLayout进行纯代码布局的理论基础
- RelativeLayout代码实现相对布局
- android 使用代码实现 RelativeLayout布局!
- android 使用代码实现 RelativeLayout布局
- Android使用代码实现RelativeLayout,LinearLayout布局
- android 使用代码实现 RelativeLayout布局
- Android使用代码实现RelativeLayout,LinearLayout布局
- Android使用代码实现RelativeLayout,LinearLayout布局
- android 使用代码实现 RelativeLayout布局
- android 使用代码实现 RelativeLayout布局
- Android使用代码实现RelativeLayout,LinearLayout布局
- 通过代码实现RelativeLayout的布局
- android 使用代码实现 RelativeLayout布局
- 代码布局relativeLayout
- RelativeLayout代码布局
- 代码布局relativeLayout
- SOUI编译
- 大型网站图片服务器架构的演进
- 框架类错误
- thinkphp整合系列之tcpdf类生成pdf文件
- android-Rect
- 纯代码RelativeLayout实现右上角布局
- mysql参数文件
- 【Get深一度】关于dB,那些你需要知道的常识
- android-Paint
- Nginx安装与简单的负载均衡(centos6.5)
- windows运行打开服务命令集合
- 内部样式表
- Java多线程 之 wait、notifyAll(十二)
- 替代Protocol buffers 的FlatBuffers:高效利用内存的序列化函数库(Unity中测试)