Android中按比例显示的布局
来源:互联网 发布:linux vi新建文件 编辑:程序博客网 时间:2024/06/06 00:08
布局的宽高按设置的比例展示 , 先给效果图:
自定义控件代码:
package com.eg.lyx.dateutil;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.widget.RelativeLayout;public class CtsRatioLayout extends RelativeLayout { private int mProportion; //宽和高的比例 public CtsRatioLayout(Context context) { this(context, null); } public CtsRatioLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CtsRatioLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); //获取比例值 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CtsRatioLayout); mProportion = a.getInt(R.styleable.CtsRatioLayout_proportion, 11); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec)); int childWidthSize = getMeasuredWidth(); int childHeightSize = getMeasuredHeight(); String s = "" + mProportion; int width_pro = Integer.parseInt(s.substring(0, s.length() / 2)); //得到宽的比例 int height_pro = Integer.parseInt(s.substring(s.length() / 2)); //得到高的比例 widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY); /**按照比例改变高度值*/ heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) (childWidthSize * height_pro * 1.0) / width_pro, MeasureSpec.EXACTLY); super.onMeasure(widthMeasureSpec, heightMeasureSpec); }}
attrs文件:
<?xml version="1.0" encoding="utf-8"?><resources xmlns:android="http://schemas.android.com/apk/res/android"> <declare-styleable name="CtsRatioLayout"> <attr name="proportion" format="integer">LYX</attr> </declare-styleable></resources>
布局文件:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:LYX="http://schemas.android.com/apk/res-auto" android:id="@+id/liveStreamingView" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--直播标题begin--> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="25dp" android:orientation="vertical"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/ctsNoTripBestTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" android:layout_marginTop="3dp" android:background="@drawable/cts_rectangle_yellow" android:paddingLeft="2dp" android:paddingRight="2dp" android:text="HOT" android:textColor="@color/white" android:textSize="13sp" android:textStyle="bold"/> <TextView android:id="@+id/liveStreamingHeadTitleTv" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:lineSpacingExtra="3dp" android:maxLines="2" android:text="旅行直播·足不出户看世界旅行直播·足不出户看世界" android:textColor="#333333" android:textSize="17sp" android:textStyle="bold"/> </FrameLayout> </LinearLayout> <!--直播标题end--> <com.eg.lyx.dateutil.CtsRatioLayout android:id="@+id/liveStreamingTwo" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="10dp" android:layout_weight="1" android:background="@drawable/two" LYX:proportion="11"> <ImageView android:id="@+id/liveStreamingPicTwoIv" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop"/> <RelativeLayout android:id="@+id/ctsNoTripLiveTwo" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="@drawable/cts_live_streaming_gradient_bg"> <ImageView android:id="@+id/ctsNoTripLocationIconTwo" android:layout_width="10dp" android:layout_height="10dp" android:layout_marginLeft="10dp" android:layout_marginTop="11dp" android:src="@drawable/cts_travel_plan_location_icon_two"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginTop="8dp" android:layout_toRightOf="@+id/ctsNoTripLocationIconTwo" android:ellipsize="end" android:singleLine="true" android:text="珠海·最美不过天边云最美不过天边云" android:textColor="#FFFFFFFF" android:textSize="12sp"/> </RelativeLayout> <ImageView android:layout_width="32dp" android:layout_height="32dp" android:layout_alignParentBottom="true" android:layout_marginBottom="10dp" android:layout_marginLeft="10dp" android:src="@drawable/cts_travel_plan_play_icon"/> </RelativeLayout> </com.eg.lyx.dateutil.CtsRatioLayout></LinearLayout>
阅读全文
1 0
- Android中按比例显示的布局
- Android 线性布局按比例显示
- Android中元素按比例布局
- Android 控件在布局中按比例放置 (
- Android 控件在布局中按比例放置
- android layout 按比例布局的代码
- android layout 按比例布局的代码
- Android按比例布局
- Android按比例布局
- Android按比例布局
- EXT的border布局如何实现按比例布局(同时可解决IE6中IFrame只显示一半问题)
- Android按比例布局之layout_weight和weightSum的使用
- Android jsoup 按比例显示图片
- Adapter中按比例显示不同高度的图片
- 按比例布局
- (转)android 按比例布局 适应不同分辨率
- Android中Bitmap按比例放大
- Qt的QGridLayout按比例设定布局
- 9.9-全栈Java笔记:遍历集合的N种方式总结&Collections工具类
- 【R的机器学习】决策树性能提升
- 使用访问二级域名加载一级域名地址,不能访问(X-Frame-Options)
- Android安全开发之正确使用Https(内含OkHttp、HttpUrlConnection配置https方式)
- 深入理解操作系统原理之设备管理
- Android中按比例显示的布局
- 队列:顺序队列&顺序循环队列、链式队列、顺序优先队列
- JVM调优之jstack找出最耗cpu的线程并定位代码分析
- tp5无法隐藏index.php入口文件
- vscode打开代码,注释中的中文显示乱码
- 领域模型中的实体类分为四种类型:VO、DTO、DO、PO
- 一台电脑上配置两台tomcat
- 寻找峰值-LintCode
- CPU性能测试工具之Super PI