1. 程式人生 > >Android 適配(百分比佈局 GridView+ListView 項佈局適配)

Android 適配(百分比佈局 GridView+ListView 項佈局適配)

百分比佈局

參考鴻洋大神的百分比佈局擴充套件,詳細請看這裡

CSDN:https://blog.csdn.net/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情況類似。具體實現可以在介面卡中增加兩個寬高百分比屬性,外部通過程式碼設定值,做到靈活應用。