百分比布局
参考鸿洋大神的百分比布局扩展,详细请看这里
优快云:https://blog.youkuaiyun.com/lmj623565791/article/details/46767825
github:https://github.com/hongyangAndroid/android-percent-support-extend
支持的布局
com.zhy.android.percent.support.PercentLinearLayout
com.zhy.android.percent.support.PercentRelativeLayout
com.zhy.android.percent.support.PercentFrameLayout
支持的属性
- layout_heightPercent
- layout_widthPercent
- layout_marginBottomPercent
- layout_marginEndPercent
- layout_marginLeftPercent
- layout_marginPercent
- layout_marginRightPercent
- layout_marginStartPercent
- layout_marginTopPercent
- layout_textSizePercent
- layout_maxWidthPercent
- layout_maxHeightPercent
- layout_minWidthPercent
- layout_minHeightPercent
- layout_paddingPercent
- layout_paddingTopPercent
- layout_paddingBottomPercent
- layout_paddingLeftPercent
- layout_paddingRightPercent
使用下来发现,基本能满足手机适配,但当屏幕宽高比例差距太大时,会有一定的拉伸,具体看自己项目实际运行环境
GridView / ListView 的适配
还有一个问题是对 GridView 跟 ListView 的项布局支持不太好,下面记录下如何适配项布局,到达百分比适配的效果
首先GridView和ListView的父类都是 AbsListView,可以在其中找到一个方法 LayoutParams()
public LayoutParams(int w, int h) {
super(w, h);
}
对布局大小的设置,在代码中设置 宽高占比
在适配器中,通过代码去设置 百分比宽高
public class SearchGirdViewAdapter extends BaseAdapter {
private Context context;
private List<String> list;
private GridView gridView;
public SearchGirdViewAdapter(Context context, List<String> departments, GridView gridView) {
this.gridView = gridView;
this.context = context;
list = new ArrayList<>();
list.addAll(departments);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View v, ViewGroup parent) {
//获取 gridView 的宽高
int width = gridView.getWidth();
int heigth = gridView.getHeight();
//设置 宽高比例
AbsListView.LayoutParams param = new AbsListView.LayoutParams((int) (width * 0.23), (int) (heigth * 0.3));
SearchGirdViewAdapter.ViewHolder viewHolder;
if (null == v) {
viewHolder = new SearchGirdViewAdapter.ViewHolder();
v = LayoutInflater.from(context).inflate(R.layout.item_fragment_search, parent, false);
v.setLayoutParams(param);
viewHolder.title = v.findViewById(R.id.search_item_title);
v.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) v.getTag();
v.setLayoutParams(param);
}
viewHolder.title.setText(list.get(position));
return v;
}
class ViewHolder {
TextView title;
}
}
主要看 getView()方法中, 通过代码设置 LayoutParams, 这里我测试代码,直接设置 宽*0.23 高*0.3(此处获取的GridView宽高大小,是在布局文件中占的大小,不是整个屏幕的大小)
项布局文件如下
<?xml version="1.0" encoding="utf-8"?>
<com.zhy.android.percent.support.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@drawable/category_genre_item_bg">
<TextView
android:id="@+id/search_item_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:ellipsize="end"
android:gravity="center"
android:maxLines="1"
android:text="电台测试"
android:textColor="#fff"
app:layout_textSizePercent="12%w" />
</com.zhy.android.percent.support.PercentRelativeLayout>
可以看到,使用的是PercentRelativelayout,中间就简单放了一个TextView
运行效果如下

红色部分,GridView显示情况,ListView情况类似。具体实现可以在适配器中增加两个宽高百分比属性,外部通过代码设置值,做到灵活应用。
356



被折叠的 条评论
为什么被折叠?



