1. 程式人生 > >一個非常好用的Android流式佈局

一個非常好用的Android流式佈局

效果圖鎮樓
這裡寫圖片描述
首先我們先新增依賴

compile 'com.zhy:flowlayout-lib:1.0.3'

然後將以下標籤和佈局新增到專案中。
主佈局layouy->activity_flow_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:zhy="http://schemas.android.com/apk/res-auto"
    xmlns:tools
="http://schemas.android.com/tools" android:id="@+id/activity_flow_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="wswy.mymob.FlowLayout">
<TextView android:layout_width="wrap_content" android:layout_height
="wrap_content" android:text="完善屬性資訊,獲取更高貸款額度" />
<com.zhy.view.flowlayout.TagFlowLayout android:id="@+id/id_flowlayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="20dp" zhy:max_select="-1" >
</com.zhy.view.flowlayout.TagFlowLayout> </LinearLayout>

填充標籤的佈局layout->tv.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="@drawable/tag_bg"
    android:text="Helloworld"
    android:textColor="@drawable/text_color">

</TextView>

我們可以在這裡來設定文字的大小,text_color設定文字的顏色、tag_bg設定標籤的背景。
在drawable->text_color中

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:color="#ff0000" android:state_checked="true"/>
    <item android:color="#f692ff"/>

</selector>

下方代表預設文字顏色,上方代表點選後的顏色.
在drawable->tag_bg中

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/checked_bg"
        android:state_checked="true"
        >

    </item>
    <item
        android:drawable="@drawable/normal_bg"></item>
</selector>

存在著兩組標籤drawable->normal_bg.xml代表的是預設時標籤的背景,drawable->checked_bg.xml代表的是點選時的背景.
在drawable->normal_bg.xml中

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffff00"/>
    <corners android:radius="30dp"/>
    <stroke android:color="#00ffff"  android:width="2dp"/>
    <padding
        android:bottom="2dp"
        android:left="10dp"
        android:right="10dp"
        android:top="2dp"/>

</shape>

上面的color 代表背景顏色,下面的color代表邊框顏色.
在drawable->checked_bg.xml中

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#ffffff"/>
    <corners android:radius="30dp"/>
    <stroke android:color="#c416ff"  android:width="2dp"/>
    <padding
        android:bottom="2dp"
        android:left="10dp"
        android:right="10dp"
        android:top="2dp"/>

</shape>

也是一樣的.
然後我們進入到.Java檔案,怕親們亂掉我將會把整個檔案內容都給大家,然後以註釋的方式為大家講解.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.Set;

public class FlowLayout extends AppCompatActivity {
    //放入流式佈局標籤中的內容
    private String[] mVals = new String[]
            {"有信用卡", "有微粒貸", "我有房", "我有車", "有社保", "有公積金",
                    "有人壽保險", "工資銀行卡轉賬", "啥都沒有"};
    private TagFlowLayout mFlowLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //獲取佈局填充器,一會將tv.xml檔案填充到標籤內.
        final LayoutInflater mInflater = LayoutInflater.from(this);
        setContentView(R.layout.activity_flow_layout);
        //初始化佈局和介面卡,直接粘就行.
        mFlowLayout = (TagFlowLayout) findViewById(R.id.id_flowlayout);
        mFlowLayout.setAdapter(new TagAdapter<String>(mVals)
        {

            @Override
            public View getView(com.zhy.view.flowlayout.FlowLayout parent, int position, String s)
            {
//                將tv.xml檔案填充到標籤內.
                TextView tv = (TextView) mInflater.inflate(R.layout.tv,
                        mFlowLayout, false);
//               為標籤設定對應的內容
                tv.setText(s);
                return tv;
            }
//             為標籤設定預點選內容(就是一開始就處於點選狀態的標籤)
            @Override
            public boolean setSelected(int position, String s)
            {
                return s.equals("Android");
            }
        });
//          為點選標籤設定點選事件.
        mFlowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener()
        {
            @Override
            public boolean onTagClick(View view, int position, com.zhy.view.flowlayout.FlowLayout parent)
            {
                Toast.makeText(FlowLayout.this, mVals[position], Toast.LENGTH_SHORT).show();
                //view.setVisibility(View.GONE);
                return true;
            }
        });

//          展示哪些標籤處於選中狀態,這個很重要我們設定標籤可點選就是為了把使用者選中狀態的標籤中的資料上傳.
        mFlowLayout.setOnSelectListener(new TagFlowLayout.OnSelectListener()
        {
            @Override
            public void onSelected(Set<Integer> selectPosSet)
            {
               setTitle("choose:" + selectPosSet.toString());
            }
        });
    }
}

親們,如果還有什麼疑問可以,在部落格下留言,我看到會回覆,如果特別急的話可以加我qq:2018866256,回覆部落格.如果對你有用的話請將這篇部落格頂起來.非常感謝.