国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android可換行的RadioGroup

這篇具有很好參考價值的文章主要介紹了Android可換行的RadioGroup。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

????????Android可換行的RadioGroup,有時候需要換行顯示的單選列表,當(dāng)然可以有多種實現(xiàn)方式,比如recycleview或者listview實現(xiàn),本文采用的是RadioGroup+rediobutton方式實現(xiàn)。由于RadioGroup僅支持水平布局與垂直布局,故需要自定義控件實現(xiàn)。

Android可換行的RadioGroup,Android,移動端,android文章來源地址http://www.zghlxwxcb.cn/news/detail-781828.html

一、首先自定義view


public class WrapRadioGroup extends RadioGroup {
    private static final String TAG = "RadioGroupEx";

    public WrapRadioGroup(Context context) {
        super(context);
    }

    public WrapRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        //調(diào)用ViewGroup的方法,測量子view
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        //最大的寬
        int maxWidth = 0;
        //累計的高
        int totalHeight = 0;

        //當(dāng)前這一行的累計行寬
        int lineWidth = 0;
        //當(dāng)前這行的最大行高
        int maxLineHeight = 0;
        //用于記錄換行前的行寬和行高
        int oldHeight;
        int oldWidth;

        int count = getChildCount();
        //假設(shè) widthMode和heightMode都是AT_MOST
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
            //得到這一行的最高
            oldHeight = maxLineHeight;
            //當(dāng)前最大寬度
            oldWidth = maxWidth;

            int deltaX = child.getMeasuredWidth() + params.leftMargin + params.rightMargin;
            if (lineWidth + deltaX + getPaddingLeft() + getPaddingRight() > widthSize) {//如果折行,height增加
                //和目前最大的寬度比較,得到最寬。不能加上當(dāng)前的child的寬,所以用的是oldWidth
                maxWidth = Math.max(lineWidth, oldWidth);
                //重置寬度
                lineWidth = deltaX;
                //累加高度
                totalHeight += oldHeight;
                //重置行高,當(dāng)前這個View,屬于下一行,因此當(dāng)前最大行高為這個child的高度加上margin
                maxLineHeight = child.getMeasuredHeight() + params.topMargin + params.bottomMargin;
//                Log.v(TAG, "maxHeight:" + totalHeight + "---" + "maxWidth:" + maxWidth);

            } else {
                //不換行,累加寬度
                lineWidth += deltaX;
                //不換行,計算行最高
                int deltaY = child.getMeasuredHeight() + params.topMargin + params.bottomMargin;
                maxLineHeight = Math.max(maxLineHeight, deltaY);
            }
            if (i == count - 1) {
                //前面沒有加上下一行的搞,如果是最后一行,還要再疊加上最后一行的最高的值
                totalHeight += maxLineHeight;
                //計算最后一行和前面的最寬的一行比較
                maxWidth = Math.max(lineWidth, oldWidth);
            }
        }

        //加上當(dāng)前容器的padding值
        maxWidth += getPaddingLeft() + getPaddingRight();
        totalHeight += getPaddingTop() + getPaddingBottom();
        setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : maxWidth,
                heightMode == MeasureSpec.EXACTLY ? heightSize : totalHeight);

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int count = getChildCount();
        //pre為前面所有的child的相加后的位置
        int preLeft = getPaddingLeft();
        int preTop = getPaddingTop();
        //記錄每一行的最高值
        int maxHeight = 0;
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();
            //r-l為當(dāng)前容器的寬度。如果子view的累積寬度大于容器寬度,就換行。
            if (preLeft + params.leftMargin + child.getMeasuredWidth() + params.rightMargin + getPaddingRight() > (r - l)) {
                //重置
                preLeft = getPaddingLeft();
                //要選擇child的height最大的作為設(shè)置
                preTop = preTop + maxHeight;
                maxHeight = getChildAt(i).getMeasuredHeight() + params.topMargin + params.bottomMargin;
            } else { //不換行,計算最大高度
                maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + params.topMargin + params.bottomMargin);
            }
            //left坐標(biāo)
            int left = preLeft + params.leftMargin;
            //top坐標(biāo)
            int top = preTop + params.topMargin;
            int right = left + child.getMeasuredWidth();
            int bottom = top + child.getMeasuredHeight();
            //為子view布局
            child.layout(left, top, right, bottom);
            //計算布局結(jié)束后,preLeft的值
            preLeft += params.leftMargin + child.getMeasuredWidth() + params.rightMargin;
        }
    }

}

二、布局直接引用

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <csu.xiaoya.robotApp.ui.view.WrapRadioGroup
            android:id="@+id/rg_bls"
            android:layout_width="438dp"
            android:layout_height="179dp"
            android:layout_below="@id/monitor_remd"
            android:layout_alignParentRight="true"
            android:layout_marginTop="@dimen/dp_15"
            android:layout_marginRight="@dimen/dp_24"
            android:orientation="horizontal"
            android:padding="1dp"
            app:maxWidth="300dp">

            <RadioButton
                android:id="@+id/rb_date_day"
                android:layout_width="@dimen/dp_84"
                android:layout_height="@dimen/dimen_48"
                android:background="@drawable/bls_am_2h_sg"
                android:button="@null"
                android:checked="true"
                android:layout_marginLeft="@dimen/dp_10"
                android:gravity="center"
                android:text="隨機(jī)血糖"
                android:textColor="@color/white"
                android:textSize="@dimen/sp_10" />

            <RadioButton
                android:id="@+id/rb_date_week"
                android:layout_width="@dimen/dp_84"
                android:layout_height="@dimen/dimen_48"
                android:layout_marginLeft="@dimen/dp_10"
                android:background="@drawable/bls_am_2h_sg"
                android:button="@null"
                android:gravity="center"
                android:text="空腹血糖"
                android:textColor="@color/white"
                android:textSize="@dimen/sp_10" />

            <RadioButton
                android:layout_width="@dimen/dp_84"
                android:layout_height="@dimen/dimen_48"
                android:layout_marginLeft="@dimen/dp_10"
                android:background="@drawable/bls_am_2h_sg"
                android:button="@null"
                android:gravity="center"
                android:text="早餐后2小時"
                android:textColor="@color/white"
                android:textSize="@dimen/sp_10" />

            <RadioButton
                android:layout_width="@dimen/dp_84"
                android:layout_height="@dimen/dimen_48"
                android:layout_marginLeft="@dimen/dp_10"
                android:background="@drawable/bls_am_2h_sg"
                android:button="@null"
                android:gravity="center"
                android:text="早餐后2小時"
                android:textColor="@color/white"
                android:textSize="@dimen/sp_10" />

            <RadioButton
                android:layout_width="@dimen/dp_84"
                android:layout_height="@dimen/dimen_48"
                android:layout_marginLeft="@dimen/dp_10"
                android:layout_marginTop="@dimen/dp_10"
                android:background="@drawable/bls_am_2h_sg"
                android:button="@null"
                android:gravity="center"
                android:text="早餐后2小時"
                android:textColor="@color/white"
                android:textSize="@dimen/sp_10" />
        </csu.xiaoya.robotApp.ui.view.WrapRadioGroup>
    </LinearLayout>

三、背景樣式bls_am_2h_sg

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:width="84dp" android:height="48dp" android:state_checked="false">
        <shape android:shape="rectangle">
            <solid android:color="#ff27b074" />
            <corners android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp" android:topLeftRadius="5dp" android:topRightRadius="5dp" />
        </shape>
    </item>
    <item android:width="88dp" android:height="50dp" android:state_checked="true">
        <shape android:shape="rectangle">
            <solid android:color="#ff27b074" />
            <corners android:bottomLeftRadius="5dp" android:bottomRightRadius="5dp" android:topLeftRadius="5dp" android:topRightRadius="5dp" />
        </shape>
    </item>
</selector>

四、大功告成

到了這里,關(guān)于Android可換行的RadioGroup的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • chatgpt賦能python:Python怎么取出換行的數(shù)據(jù)?

    在數(shù)據(jù)分析和處理中,我們常常需要從文件中讀取數(shù)據(jù),特別是當(dāng)數(shù)據(jù)量很大時更是如此。在Python中,讀取文本文件中的數(shù)據(jù)很簡單,但是有時候讀取的文本文件中可能含有換行符,這可能會給數(shù)據(jù)處理造成麻煩。因此,本篇文章將介紹如何使用Python取出換行符的數(shù)據(jù),以便

    2024年02月08日
    瀏覽(21)
  • 無涯教程-Android - RadioGroup函數(shù)

    RadioGroup類用于單選按鈕集。 如果我們選中屬于某個單選按鈕組的一個單選按鈕,它將自動取消選中同一組中以前選中的任何單選按鈕。 以下是與RadioGroup控制相關(guān)的重要屬性。您可以查看Android官方文檔以獲取屬性的完整列表以及可以在運行時更改這些屬性的相關(guān)方法。 屬性

    2024年02月10日
    瀏覽(18)
  • Android Jetpack Compose之RadioGroup的使用

    Android Jetpack Compose之RadioGroup的使用

    Android Jetpack Compose是一個現(xiàn)代化的UI工具包,幫助開發(fā)者以聲明式的方式構(gòu)建出美觀且功能強(qiáng)大的Android應(yīng)用。在本文中,我們將詳細(xì)介紹其中的一個重要組件—— RadioGroup 。 一. RadioGroup簡介 Jetpack Compose中并沒有像傳統(tǒng)View系統(tǒng)中那樣直接提供 RadioGroup ,但我們可以很方便地通

    2024年02月06日
    瀏覽(101)
  • 【Android入門到項目實戰(zhàn)-- 11.2】—— 實現(xiàn)底部導(dǎo)航欄(RadioGroup+Fragment)

    【Android入門到項目實戰(zhàn)-- 11.2】—— 實現(xiàn)底部導(dǎo)航欄(RadioGroup+Fragment)

    ????????效果如下,使用RadioGroup實現(xiàn),不能左右滑動切換頁面,適用于導(dǎo)航頁里還有需要切換頁面的場景,如果需要滑動效果,使用ViewPager實現(xiàn)。 ??????? 以下示例按照圖上實現(xiàn),具體多少個頁面,按需修改。 ??????? 由于需要用到icon,提前下載好圖標(biāo)到drawable文件

    2024年02月10日
    瀏覽(27)
  • QT 實現(xiàn)tablewidget整行的上下移動和雙擊編輯

    QT 實現(xiàn)tablewidget整行的上下移動和雙擊編輯

    一、效果展示 二、實現(xiàn)方法 1、先對tablewidget設(shè)置 2、實現(xiàn)行的上下移動 主要實現(xiàn)方式是通過交換兩行的數(shù)據(jù)來實現(xiàn)的 下面這兩句主要實現(xiàn)選擇行跟隨移動的行

    2024年02月11日
    瀏覽(21)
  • HTML元素中有中文、英文、符號、數(shù)字。第一行沒排滿就自動換行的解決辦法:word-break:break-all的使用

    HTML元素中有中文、英文、符號、數(shù)字。第一行沒排滿就自動換行的解決辦法:word-break:break-all的使用

    word-break: break-all 是一個CSS屬性,用于控制文本在容器中的換行方式。它的作用是強(qiáng)制在任意字符之間進(jìn)行換行,即使這樣可能會導(dǎo)致單詞被分割。 具體來說, word-break 屬性有以下幾個取值: normal (默認(rèn)值):默認(rèn)的換行行為。單詞不會被分割,會根據(jù)容器的寬度自動換行。

    2024年02月15日
    瀏覽(22)
  • Android 在TextView前面添加多個任意View且不影響換行

    Android 在TextView前面添加多個任意View且不影響換行

    實現(xiàn)效果如下: 如上,將頭像后面的東西看作一個整體,因為不能影響后面內(nèi)容的換行,且前面控件的長度是可變的,所以采用自定義View的方法來實現(xiàn): 使用舉例 :? (??抱歉啊使用這邊沒有用Java寫,不會Kotlin的應(yīng)該也能看懂啥意思) activity_main.xml: la: dataListType的 0,1,

    2024年02月09日
    瀏覽(19)
  • Qt編寫精美輸入法(歷時十年迭代/可換膚/支持Qt4/5/6/win/linux/mac/嵌入式等)

    Qt編寫精美輸入法(歷時十年迭代/可換膚/支持Qt4/5/6/win/linux/mac/嵌入式等)

    大概是從2012年就開始研究用Qt寫輸入法,因為項目需要,嵌入式板子上,沒有對應(yīng)的輸入法,當(dāng)初使用過很多NVR,里面也是鼠標(biāo)按下彈出輸入法面板進(jìn)行輸入,可以切換數(shù)字和字母及中文,于是借鑒著操作交互流程,用純QWidget代碼實現(xiàn)一個,當(dāng)然最初的版本是非常簡單和丑陋

    2024年02月09日
    瀏覽(21)
  • Qt/C++編寫精美輸入法(歷時十年迭代/可換膚/支持Qt4/5/6/win/linux/mac/嵌入式等)

    Qt/C++編寫精美輸入法(歷時十年迭代/可換膚/支持Qt4/5/6/win/linux/mac/嵌入式等)

    大概是從2012年就開始研究用Qt寫輸入法,因為項目需要,嵌入式板子上,沒有對應(yīng)的輸入法,當(dāng)初使用過很多NVR,里面也是鼠標(biāo)按下彈出輸入法面板進(jìn)行輸入,可以切換數(shù)字和字母及中文,于是借鑒著操作交互流程,用純QWidget代碼實現(xiàn)一個,當(dāng)然最初的版本是非常簡單和丑陋

    2024年02月12日
    瀏覽(89)
  • Android:自定義沿著曲線軌跡移動

    Android:自定義沿著曲線軌跡移動

    前幾天,后臺有老鐵留言,說有個需求,畫兩條曲線,中間是一個小球,沿著兩條線中間的軌跡從左往右移動,讓提供個思路,做為一個極度寵粉的博主,思路不僅要提供,實現(xiàn)方案也必須要給出,在互聯(lián)網(wǎng)中玩的就是真實! 今天的文章大致如下: 1、最終實現(xiàn)效果 2、思路

    2024年02月13日
    瀏覽(24)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包