[求助]radiobutton如何支持换行支持动态添加支持margin如何支持style

吉凶以情迁 2015-12-11 10:45:04
第一个问题:
网上有很多支持换行的flowradiogroup有的都没有效果,
有的呢则不支持里面的控件设置margin属性,挤成一坨,而且动态addView发现也丧失了流式功能。
另外style属性,网上说的setTextAppeare**根本不行,但是android底层实现了,我看了源码看不懂没看了,没找到具体代码。
...全文
343 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
吉凶以情迁 2015-12-15
  • 打赏
  • 举报
回复

package com.meimi.youganjue.ui;


import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;

import com.meimi.youganjue.dao.MyPair;

import java.util.ArrayList;
import java.util.Iterator;

public class FlowRadioGroup extends BaseFlowRadioGroup
{

	private static final String TAG = "FlowRadioGroup";


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



	private boolean singleSelection;
	private String mKey;

	public String getKey() {
		return mKey;
	}

	public void setKey(String key) {
		this.mKey = key;
		keyValues.setFirst(getKey());
	}


	/**
	 * 是否单选
	 * @return
	 */
	public boolean isSingleSelection() {
		return singleSelection;
	}

	public void setSingleSelection(boolean singleSelection) {
		this.singleSelection = singleSelection;
	}

	MyPair<String,ArrayList> keyValues=new MyPair<>();
	{
		keyValues.setSecond(new ArrayList<String>());
	}

	public void setValue(String value){
		ArrayList<String> list = keyValues.getSecond();
		if(isSingleSelection()){
			list.clear();
		}else{
		}
		list.add(value);
	}
	public ArrayList<String> getValue(){
		return keyValues.getSecond();
	}

	/**
	 * 正对多选
	 * @param value
	 */
	public void removeValue(String value) {
		ArrayList<String> list = keyValues.getSecond();
		for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) {
			String str = iterator.next();
			if (str.equalsIgnoreCase(value)) {
				Log.i(TAG, "DELETE:" + value);
				iterator.remove();
			}
		}
	}
}

这个支持xml的方式包含控件,但是如果是动态添加的依然不行 动态添加的具体代码是;

     for (String s : taglist) {

                        RadioButton radioButton = (RadioButton) LayoutInflater.from(this).inflate(R.layout.view_radiobutton, tagGroup,false);
                        radioButton.setText(""+s);
                        tagGroup.addTagView(radioButton);
                    }
xml是:
<RadioButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/radioBtnStyleTemplete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:text="你好世界"
    >

</RadioButton>
style是:

    <style name="radioBtnStyle">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_weight">1</item>
        <item name="android:background">@android:color/transparent</item>
        <item name="android:button">@null</item>
        <item name="android:gravity">center_horizontal|center_vertical</item>
        <item name="android:textColor">@color/color_btn_selector</item>
    </style>
吉凶以情迁 2015-12-15
  • 打赏
  • 举报
回复
引用 3 楼 baiqiantao 的回复:
怎么不能设置呢,看看上图的效果,不管是margin还是padding都可以设置的
不行,不支持代码添加的方式,我是动态添加的radiobutton ,checkbox,始终不行, 不信你试试。 由于要用样式我用的是inflate,最后一个参数我也设置了false,反正就是不行,而且我要做的是排斥功能,
吉凶以情迁 2015-12-15
  • 打赏
  • 举报
回复
不行,不支持代码添加的方式,我是动态添加的radiobutton ,checkbox,始终不行,
吉凶以情迁 2015-12-11
  • 打赏
  • 举报
回复
这个支持动态添加代码以及换行,但是有一个毛病就是不支持margin属性,设置的都无效了。不知道怎么破..


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
    int childCount = getChildCount();
    int x = 0;
    int y = 0;
    int row = 0;

    for (int index = 0; index < childCount; index++) {
        final View child = getChildAt(index);
        if (child.getVisibility() != View.GONE) {
            child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
            // 此处增加onlayout中的换行判断,用于计算所需的高度
            int width = child.getMeasuredWidth();
            int height = child.getMeasuredHeight();
            x += width;
            y = row * height + height;
            if (x > maxWidth) {
                x = width;
                row++;
                y = row * height + height;
            }
        }
    }
    // 设置容器所需的宽度和高度
    setMeasuredDimension(maxWidth, y);
}

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int childCount = getChildCount();
        int maxWidth = r - l;
        int x = 0;
        int y = 0;
        int row = 0;
        for (int i = 0; i < childCount; i++) {
            final View child = this.getChildAt(i);
            if (child.getVisibility() != View.GONE) {
                int width = child.getMeasuredWidth();
                int height = child.getMeasuredHeight();
                x += width;
                y = row * height + height;
                if (x > maxWidth) {
                    x = width;
                    row++;
                    y = row * height + height;
                }
                child.layout(x - width, y - height, x, y);
            }
        }
    }

baiqiantao 2015-12-11
  • 打赏
  • 举报
回复
baiqiantao 2015-12-11
  • 打赏
  • 举报
回复
怎么不能设置呢,看看上图的效果,不管是margin还是padding都可以设置的

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧