Android 找不到以下设计的布局
所有按钮文本都是从服务器动态生成的。我已经尝试了所有基本的给定布局,以形成这个设计,但无法得到预期的结果。请帮助我,我如何在android中解决这个设计实现Android 找不到以下设计的布局,android,android-layout,android-xml,android-button,Android,Android Layout,Android Xml,Android Button,所有按钮文本都是从服务器动态生成的。我已经尝试了所有基本的给定布局,以形成这个设计,但无法得到预期的结果。请帮助我,我如何在android中解决这个设计实现 您需要的小部件是自动调整TextView的大小 试试下面的课,它可能会帮助你 import android.annotation.TargetApi; import android.content.Context; import android.content.res.Resources; import android.graphics.R
您需要的小部件是自动调整TextView的大小 试试下面的课,它可能会帮助你
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.RectF;
import android.os.Build;
import android.text.Layout.Alignment;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.SparseIntArray;
import android.util.TypedValue;
import android.widget.TextView;
public class AutoResizeTextView extends TextView {
private interface SizeTester {
/**
*
* @param suggestedSize
* Size of text to be tested
* @param availableSpace
* available space in which text must fit
* @return an integer < 0 if after applying {@code suggestedSize} to
* text, it takes less space than {@code availableSpace}, > 0
* otherwise
*/
public int onTestSize(int suggestedSize, RectF availableSpace);
}
private RectF mTextRect = new RectF();
private RectF mAvailableSpaceRect;
private SparseIntArray mTextCachedSizes;
private TextPaint mPaint;
private float mMaxTextSize;
private float mSpacingMult = 1.0f;
private float mSpacingAdd = 0.0f;
private float mMinTextSize = 20;
private int mWidthLimit;
private static final int NO_LINE_LIMIT = -1;
private int mMaxLines;
private boolean mEnableSizeCache = true;
private boolean mInitializedDimens;
public AutoResizeTextView(Context context) {
super(context);
initialize();
}
public AutoResizeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}
public AutoResizeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}
private void initialize() {
mPaint = new TextPaint(getPaint());
mMaxTextSize = getTextSize();
mAvailableSpaceRect = new RectF();
mTextCachedSizes = new SparseIntArray();
if (mMaxLines == 0) {
// no value was assigned during construction
mMaxLines = NO_LINE_LIMIT;
}
}
@Override
public void setTextSize(float size) {
mMaxTextSize = size;
mTextCachedSizes.clear();
adjustTextSize();
}
@Override
public void setMaxLines(int maxlines) {
super.setMaxLines(maxlines);
mMaxLines = maxlines;
adjustTextSize();
}
public int getMaxLines() {
return mMaxLines;
}
@Override
public void setSingleLine() {
super.setSingleLine();
mMaxLines = 1;
adjustTextSize();
}
@Override
public void setSingleLine(boolean singleLine) {
super.setSingleLine(singleLine);
if (singleLine) {
mMaxLines = 1;
} else {
mMaxLines = NO_LINE_LIMIT;
}
adjustTextSize();
}
@Override
public void setLines(int lines) {
super.setLines(lines);
mMaxLines = lines;
adjustTextSize();
}
@Override
public void setTextSize(int unit, float size) {
Context c = getContext();
Resources r;
if (c == null)
r = Resources.getSystem();
else
r = c.getResources();
mMaxTextSize = TypedValue.applyDimension(unit, size,
r.getDisplayMetrics());
mTextCachedSizes.clear();
adjustTextSize();
}
@Override
public void setLineSpacing(float add, float mult) {
super.setLineSpacing(add, mult);
mSpacingMult = mult;
mSpacingAdd = add;
}
/**
* Set the lower text size limit and invalidate the view
*
* @param minTextSize
*/
public void setMinTextSize(float minTextSize) {
mMinTextSize = minTextSize;
adjustTextSize();
}
private void adjustTextSize() {
if (!mInitializedDimens) {
return;
}
int startSize = (int) mMinTextSize;
int heightLimit = getMeasuredHeight() - getCompoundPaddingBottom()
- getCompoundPaddingTop();
mWidthLimit = getMeasuredWidth() - getCompoundPaddingLeft()
- getCompoundPaddingRight();
mAvailableSpaceRect.right = mWidthLimit;
mAvailableSpaceRect.bottom = heightLimit;
super.setTextSize(
TypedValue.COMPLEX_UNIT_PX,
efficientTextSizeSearch(startSize, (int) mMaxTextSize,
mSizeTester, mAvailableSpaceRect));
}
private final SizeTester mSizeTester = new SizeTester() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public int onTestSize(int suggestedSize, RectF availableSPace) {
mPaint.setTextSize(suggestedSize);
String text = getText().toString();
boolean singleline = getMaxLines() == 1;
if (singleline) {
mTextRect.bottom = mPaint.getFontSpacing();
mTextRect.right = mPaint.measureText(text);
} else {
StaticLayout layout = new StaticLayout(text, mPaint,
mWidthLimit, Alignment.ALIGN_NORMAL, mSpacingMult,
mSpacingAdd, true);
// return early if we have more lines
if (getMaxLines() != NO_LINE_LIMIT
&& layout.getLineCount() > getMaxLines()) {
return 1;
}
mTextRect.bottom = layout.getHeight();
int maxWidth = -1;
for (int i = 0; i < layout.getLineCount(); i++) {
if (maxWidth < layout.getLineWidth(i)) {
maxWidth = (int) layout.getLineWidth(i);
}
}
mTextRect.right = maxWidth;
}
mTextRect.offsetTo(0, 0);
if (availableSPace.contains(mTextRect)) {
// may be too small, don't worry we will find the best match
return -1;
} else {
// too big
return 1;
}
}
};
/**
* Enables or disables size caching, enabling it will improve performance
* where you are animating a value inside TextView. This stores the font
* size against getText().length() Be careful though while enabling it as 0
* takes more space than 1 on some fonts and so on.
*
* @param enable
* enable font size caching
*/
public void enableSizeCache(boolean enable) {
mEnableSizeCache = enable;
mTextCachedSizes.clear();
adjustTextSize(getText().toString());
}
private int efficientTextSizeSearch(int start, int end,
SizeTester sizeTester, RectF availableSpace) {
if (!mEnableSizeCache) {
return binarySearch(start, end, sizeTester, availableSpace);
}
int key = getText().toString().length();
int size = mTextCachedSizes.get(key);
if (size != 0) {
return size;
}
size = binarySearch(start, end, sizeTester, availableSpace);
mTextCachedSizes.put(key, size);
return size;
}
private static int binarySearch(int start, int end, SizeTester sizeTester,
RectF availableSpace) {
int lastBest = start;
int lo = start;
int hi = end - 1;
int mid = 0;
while (lo <= hi) {
mid = (lo + hi) >>> 1;
int midValCmp = sizeTester.onTestSize(mid, availableSpace);
if (midValCmp < 0) {
lastBest = lo;
lo = mid + 1;
} else if (midValCmp > 0) {
hi = mid - 1;
lastBest = hi;
} else {
return mid;
}
}
// make sure to return last best
// this is what should always be returned
return lastBest;
}
@Override
protected void onTextChanged(final CharSequence text, final int start,
final int before, final int after) {
super.onTextChanged(text, start, before, after);
adjustTextSize();
}
@Override
protected void onSizeChanged(int width, int height, int oldwidth,
int oldheight) {
mInitializedDimens = true;
mTextCachedSizes.clear();
super.onSizeChanged(width, height, oldwidth, oldheight);
if (width != oldwidth || height != oldheight) {
adjustTextSize();
}
}
}
导入android.annotation.TargetApi;
导入android.content.Context;
导入android.content.res.Resources;
导入android.graphics.RectF;
导入android.os.Build;
导入android.text.Layout.Alignment;
导入android.text.StaticLayout;
导入android.text.TextPaint;
导入android.util.AttributeSet;
导入android.util.SparseIntArray;
导入android.util.TypedValue;
导入android.widget.TextView;
公共类AutoResizeTextView扩展了TextView{
专用接口SizeTester{
/**
*
*@param suggestedSize
*待测试文本的大小
*@param可用空间
*文本必须适合的可用空间
*@将{@code suggestedSize}应用于后返回一个小于0的整数
*文本,它占用的空间小于{@code availableSpace},>0
*否则
*/
公共int-onTestSize(int-suggestedSize,RectF-availableSpace);
}
private RectF mtextect=new RectF();
私有RectF mavaailablespacelect;
私人SparseIntArray mTextCachedSizes;
私人文本点;
私有浮动mMaxTextSize;
私人浮动mSpacingMult=1.0f;
私人浮动mSpacingAdd=0.0f;
专用浮点数mMinTextSize=20;
私人国际私人有限公司;
私有静态最终整数无线限制=-1;
私人互联网;
私有布尔值mEnableSizeCache=true;
私有布尔最小化的D门;
公共AutoResizeTextView(上下文){
超级(上下文);
初始化();
}
公共AutoResizeTextView(上下文、属性集属性){
超级(上下文,attrs);
初始化();
}
公共AutoResizeTextView(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
初始化();
}
私有void初始化(){
mPaint=newtextpaint(getPaint());
mmaxtsize=getTextSize();
mavaailablespacelect=new RectF();
mTextCachedSizes=新的SparseIntArray();
如果(mMaxLines==0){
//在构建过程中未分配任何值
mMaxLines=无线路限制;
}
}
@凌驾
公共void setTextSize(浮动大小){
mmaxtsize=大小;
mTextCachedSizes.clear();
adjustTextSize();
}
@凌驾
公共void setMaxLines(int maxlines){
super.setMaxLines(maxlines);
mMaxLines=最大线;
adjustTextSize();
}
公共int getMaxLines(){
返回mMaxLines;
}
@凌驾
公共无效设置单行(){
super.setSingleLine();
mMaxLines=1;
adjustTextSize();
}
@凌驾
公共无效设置单线(布尔单线){
超级设置单线(单线);
如果(单线){
mMaxLines=1;
}否则{
mMaxLines=无线路限制;
}
adjustTextSize();
}
@凌驾
公共无效设置行(整数行){
超级设置行(行);
mMaxLines=直线;
adjustTextSize();
}
@凌驾
公共void setTextSize(整数单位,浮点大小){
Context c=getContext();
资源r;
如果(c==null)
r=Resources.getSystem();
其他的
r=c.getResources();
mmaxtsize=TypedValue.applyDimension(单位、大小、,
r、 getDisplayMetrics());
mTextCachedSizes.clear();
adjustTextSize();
}
@凌驾
公共void设置行间距(浮动添加、浮动多个){
super.setlinespace(添加,多个);
mSpacingMult=mult;
mSpacingAdd=添加;
}
/**
*设置较低的文本大小限制并使视图无效
*
*@param minTextSize
*/
公共void setMinTextSize(浮点minTextSize){
mMinTextSize=minTextSize;
adjustTextSize();
}
私有void adjustTextSize(){
如果(!minitializeddmens){
返回;
}
int startSize=(int)mMinTextSize;
int heightLimit=getMeasuredHeight()-getCompoundPaddingBottom()
-getCompoundPaddingTop();
mWidthLimit=getMeasuredWidth()-getCompoundPaddingLeft()
-getCompoundPaddingRight();
mavaailablespacelect.right=mWidthLimit;
MavaailableSpacelect.bottom=高度限制;
super.setTextSize(
TypedValue.COMPLEX\u UNIT\u PX,
EfficientExtSizeSearch(起始尺寸,(内部)mm最大尺寸,
msizetter,mavaailable(等);
}
private final SizeTester mSizeTester=新SizeTester(){
@TargetApi(Build.VERSION\u code.JELLY\u BEAN)
@凌驾
公共int-onTestSize(int-suggestedSize,RectF-availableSPace){
mPaint.setTextSize(建议大小);
字符串text=getText().toString();
布尔单线=getMaxLines()==1;
如果(单线){
mtextect.bottom=mPaint.getFontSpacing();
mtextect.right=mPaint.measureText(文本);
}否则{
StaticLayout=新的StaticLayout(文本、mPaint、,
mWidthLimit,Alignment.ALIGN_NORMAL,mSpacingMult,
mSpacingAdd,正确);
//如果我们有更多的线路,早点回来
如果(getMaxLines()!=无线限制
&&layout.getLineCount()>getMaxLines()){
返回1;
}
mtextect.bottom=layout.getHeight();
int maxWidth=-1;
对于(int i=0;i<LinearLayout
android:id="@+id/tagviews"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
tagViews= (LinearLayout)findViewById(R.id.tagviews);
tagViews.removeAllViews();
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
LinearLayout childView;
int remainingWidth, count = tagViews.getChildCount();
if (count <= 0)
tagViews.addView(ll);
TextView tagText;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(5, 5, 5, 5);
int width = 0;
WindowManager w = ((Activity) context).getWindowManager();
Point size = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
w.getDefaultDisplay().getSize(size);
width = size.x;
} else {
// Display d = w.getDefaultDisplay();
// width = d.getWidth();
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
width = displaymetrics.widthPixels;
//int screenHeight = displaymetrics.heightPixels;
}
try {
// you might have some array from where you want to set Tags
for (int k = 0; k < arr.length(); k++) {
JSONObject obj = arr.getJSONObject(k);
final String name = obj.getString("name");
for (int i = 0; i < tagViews.getChildCount(); i++) {
childView = (LinearLayout) tagViews.getChildAt(i);
tagText = new TextView(context);
tagText.setText(name);
tagText.setLayoutParams(params);
tagText.setPadding(10, 5, 10, 5);
tagText.setBackgroundColor(Color.rgb(0xee, 0xee, 0xee));
tagText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// for handling clicks on tags
}
});
// count = tagViews.getChildCount();
childView.measure(0, 0);
remainingWidth = width - childView.getMeasuredWidth();
//// Log.v("subcriptionAdapter", "remaining width=" + remainingWidth + " childview width= " + childView.getMeasuredWidth());
try {
if (remainingWidth > childView.getMeasuredWidth()) {
childView.addView(tagText);
childView.invalidate();
break;
} else if ((i == tagViews.getChildCount() - 1) && (remainingWidth < childView.getMeasuredWidth())) {
childView = new LinearLayout(context);
childView.setOrientation(LinearLayout.HORIZONTAL);
childView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
childView.addView(tagText);
tagViews.addView(childView);
childView.invalidate();
break;
} else {
continue;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
// TODO: handle exception
}
tagViews.invalidate();
public class MainChipViewAdapter extends ChipViewAdapter {
public MainChipViewAdapter(Context context) {
super(context);
}
@Override
public int getLayoutRes(int position) {
Tag tag = (Tag) getChip(position);
switch (tag.getType()) {
default:
case 2:
case 4:
return 0;
case 1:
case 5:
return R.layout.chip_double_close;
case 3:
return R.layout.chip_close;
}
}