android.widget.TextView#measure ( )源码实例Demo

下面列出了android.widget.TextView#measure ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: RemoteControlView   文件: RemoteControlView.java
private void init(Context context) {
    setWillNotDraw(false);
    mPhonePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mBackPath = new Path();
    // 不使用硬件加速,否则虚线显示不出
    setLayerType(LAYER_TYPE_SOFTWARE, null);
    // 拖拽有效区域
    frameLayout = new FrameLayout(context);
    frameLayout.setBackgroundColor(Color.parseColor(CONTENT_COLOR));
    frameLayout.setOnDragListener(this);
    addView(frameLayout);
    // 提示文字
    mTextView = new TextView(context);
    mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
    mTextView.setTextColor(Color.WHITE);
    mTextView.setText("长按并拖拽下方按钮到这里");
    LayoutParams fl = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    fl.gravity = Gravity.CENTER;
    mTextView.setLayoutParams(fl);
    mTextView.measure(0, 0);
    addView(mTextView);
}
 
private int computeStableWidth(TextView v, int maxNumber) {
    int maxWidth = 0;

    for (int i = 0; i < maxNumber; i++) {
        final String text = String.format("%02d", i);
        v.setText(text);
        v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

        final int width = v.getMeasuredWidth();
        if (width > maxWidth) {
            maxWidth = width;
        }
    }

    return maxWidth;
}
 
源代码3 项目: android-chromium   文件: TranslateLanguagePanel.java
/** Measures how large the view needs to be to avoid truncating its children. */
public void measureWidthRequiredForView() {
    mMinimumWidth = 0;

    final int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

    FrameLayout layout = new FrameLayout(getContext());
    TextView estimator = (TextView) LayoutInflater.from(getContext()).inflate(
            R.layout.infobar_text, null);
    layout.addView(estimator);
    for (int i = 0; i < getCount(); ++i) {
        estimator.setText(getStringForLanguage(i));
        estimator.measure(spec, spec);
        mMinimumWidth = Math.max(mMinimumWidth, estimator.getMeasuredWidth());
    }
}
 
源代码4 项目: CoolSignIn   文件: TextSharedElementCallback.java
@Override
public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements,
                               List<View> sharedElementSnapshots) {
    TextView initialView = getTextView(sharedElements);

    if (initialView == null) {
        Log.w(TAG, "onSharedElementEnd: No shared TextView, skipping");
        return;
    }

    // Setup the TextView's end values.
    initialView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTargetViewTextSize);
    ViewUtils.setPaddingStart(initialView, mTargetViewPaddingStart);

    // Re-measure the TextView (since the text size has changed).
    int widthSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    initialView.measure(widthSpec, heightSpec);
    initialView.requestLayout();
}
 
@NonNull
private BitmapDescriptor createClusterIcon(int clusterBucket) {
    @SuppressLint("InflateParams")
    TextView clusterIconView = (TextView) LayoutInflater.from(mContext)
            .inflate(R.layout.map_cluster_icon, null);
    clusterIconView.setBackground(createClusterBackground());
    clusterIconView.setTextColor(mIconStyle.getClusterTextColor());
    clusterIconView.setTextSize(TypedValue.COMPLEX_UNIT_PX,
            mIconStyle.getClusterTextSize());

    clusterIconView.setText(getClusterIconText(clusterBucket));

    clusterIconView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    clusterIconView.layout(0, 0, clusterIconView.getMeasuredWidth(),
            clusterIconView.getMeasuredHeight());

    Bitmap iconBitmap = Bitmap.createBitmap(clusterIconView.getMeasuredWidth(),
            clusterIconView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(iconBitmap);
    clusterIconView.draw(canvas);

    return BitmapDescriptorFactory.fromBitmap(iconBitmap);
}
 
源代码6 项目: candybar-library   文件: Popup.java
private int getMeasuredWidth(@NonNull Context context) {
    DisplayMetrics metrics = new DisplayMetrics();
    ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int maxWidth = context.getResources().getDimensionPixelSize(R.dimen.popup_max_width);
    int minWidth = context.getResources().getDimensionPixelSize(R.dimen.popup_min_width);
    String longestText = "";
    for (PopupItem item : mAdapter.getItems()) {
        if (item.getTitle().length() > longestText.length())
            longestText = item.getTitle();
    }

    int padding = context.getResources().getDimensionPixelSize(R.dimen.content_margin);
    int iconSize = context.getResources().getDimensionPixelSize(R.dimen.icon_size_small);
    TextView textView = new TextView(context);
    textView.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    textView.setTypeface(TypefaceHelper.getRegular(context));
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getResources()
            .getDimension(R.dimen.text_content_subtitle));
    textView.setPadding(padding + iconSize + padding, 0, padding, 0);
    textView.setText(longestText);

    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(metrics.widthPixels, View.MeasureSpec.AT_MOST);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(widthMeasureSpec, heightMeasureSpec);

    int measuredWidth = textView.getMeasuredWidth() + padding;
    if (measuredWidth <= minWidth) {
        return minWidth;
    }

    if (measuredWidth >= minWidth && measuredWidth <= maxWidth) {
        return measuredWidth;
    }
    return maxWidth;
}
 
源代码7 项目: LemonBubble4Android   文件: LemonBubbleInfo.java
/**
 * 获取指定的textV的行高
 *
 * @param textView 要获取的textView的行高
 * @return textView的每行的高度
 */
int getTitleHeight(TextView textView, int viewWidth) {
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(_PST.dpToPx(viewWidth), View.MeasureSpec.AT_MOST);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(widthMeasureSpec, heightMeasureSpec);
    return _PST.pxToDp(textView.getMeasuredHeight());
}
 
源代码8 项目: AndroidWallet   文件: NoPaddingTextView.java
private int measureHeight(String text, int widthMeasureSpec) {
    float textSize = getTextSize();

    TextView textView = new TextView(getContext());
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
    textView.setText(text);
    textView.measure(widthMeasureSpec, 0);
    return textView.getMeasuredHeight();
}
 
源代码9 项目: AndroidWallet   文件: NoPaddingTextView.java
private int getAdditionalPadding() {
    float textSize = getTextSize();

    TextView textView = new TextView(getContext());
    textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
    textView.setLines(1);
    textView.measure(0, 0);
    int measuredHeight = textView.getMeasuredHeight();
    if (measuredHeight - textSize > 0) {
        mAdditionalPadding = (int) (measuredHeight - textSize);
        Log.v("NoPaddingTextView", "onMeasure: height=" + measuredHeight + " textSize=" + textSize + " mAdditionalPadding=" + mAdditionalPadding);
    }
    return mAdditionalPadding;
}
 
源代码10 项目: EdXposedManager   文件: LogsAdapter.java
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
    TextView view = holder.textView;
    view.setText(logs.get(position));
    view.measure(0, 0);
    int desiredWidth = view.getMeasuredWidth();
    ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
    layoutParams.width = desiredWidth;
    if (recyclerView.getWidth() < desiredWidth) {
        recyclerView.requestLayout();
    }
}
 
源代码11 项目: lua-for-android   文件: AutoCompletePanel.java
public int getItemHeight() {
    if (_h != 0)
        return _h;

    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    TextView item = (TextView) inflater.inflate(android.R.layout.simple_list_item_1, null);
    item.measure(0, 0);
    _h = item.getMeasuredHeight();
    return _h;
}
 
源代码12 项目: Simple-Solitaire   文件: Game.java
/**
 * Create a textView and add it to the given layout (game content). Used to add custom texts
 * to a game. This also sets the text apperance to AppCompat and the gravity to center.
 * The width and height is also measured, so you can use it directly.
 *
 * @param width   The width to apply to the
 * @param layout  he textView will be added to this layout
 * @param context Context to create view
 */
protected void addTextViews(int count, int width, RelativeLayout layout, Context context) {

    for (int i = 0; i < count; i++) {
        TextView textView = new TextView(context);
        textView.setWidth(width);
        TextViewCompat.setTextAppearance(textView, R.style.TextAppearance_AppCompat);
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(textViewColor);
        layout.addView(textView);
        textView.measure(0, 0);
        textViews.add(textView);
    }
}
 
源代码13 项目: miappstore   文件: AppIntroHolder.java
/**
 * 获取10行文本的高度
 * @return
 */
public int getShortMeasureHeight() {
    TextView textView = new TextView(UiUtils.getContext());
    textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP,14);
    textView.setLines(10);//表示有十行数据
    //开始宽度
    int width = tv_content.getMeasuredWidth();
    //指定测量规则
    int measureWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
    int measureHeight = View.MeasureSpec.makeMeasureSpec(1000, View.MeasureSpec.AT_MOST);
    //开始测量
    textView.measure(measureWidth, measureHeight);
    return textView.getMeasuredHeight();
}
 
private void layoutDebugInfo(final int positionInStrip, final ViewGroup placerView,
        final int x) {
    final TextView debugInfoView = mDebugInfoViews.get(positionInStrip);
    final CharSequence debugInfo = debugInfoView.getText();
    if (debugInfo == null) {
        return;
    }
    placerView.addView(debugInfoView);
    debugInfoView.measure(
            ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    final int infoWidth = debugInfoView.getMeasuredWidth();
    final int y = debugInfoView.getMeasuredHeight();
    ViewLayoutUtils.placeViewAt(
            debugInfoView, x - infoWidth, y, infoWidth, debugInfoView.getMeasuredHeight());
}
 
@Before
public void setup() {
    if (Looper.myLooper() == null) {
        Looper.prepare();
    }

    context = InstrumentationRegistry.getTargetContext();
    recyclerView = mock(RecyclerView.class);
    adapter = mock(BrickRecyclerAdapter.class);
    view = mock(View.class);
    dataManager = mock(BrickDataManager.class);

    when(dataManager.getBrickRecyclerAdapter()).thenReturn(adapter);
    when(adapter.getRecyclerView()).thenReturn(recyclerView);

    itemView = new TextView(context);
    ((TextView) itemView).setText(TEST);
    itemView.measure(MOCK_VIEW_SIZE, MOCK_VIEW_SIZE);

    stickyViewHolder = new BrickViewHolder(itemView);

    stickyHolderContainer = spy((ViewGroup) LayoutInflater.from(context).inflate(R.layout.vertical_header, new LinearLayout(context), false));
    stickyHolderContainer.layout(0, 0, MOCK_VIEW_SIZE, MOCK_VIEW_SIZE);
    stickyHolderContainer.setLayoutParams(new ViewGroup.LayoutParams(MOCK_VIEW_SIZE, MOCK_VIEW_SIZE));

    View header = spy((ViewGroup) LayoutInflater.from(context).inflate(R.layout.text_brick, new LinearLayout(context), false));
    header.layout(0, 0, MOCK_VIEW_SIZE, MOCK_VIEW_SIZE);
    header.setLayoutParams(new ViewGroup.LayoutParams(MOCK_VIEW_SIZE, MOCK_VIEW_SIZE));

    stickyHolderLayout = ((ViewGroup) stickyHolderContainer.findViewById(R.id.header_sticky_layout));
    stickyHolderLayout.addView(header);

    headerBehavior = spy(new TestStickyHeaderBehavior(dataManager, stickyHolderContainer));
    headerBehavior.swapStickyView(stickyViewHolder);
    headerBehavior.translateStickyView();
    headerBehavior.setStickyBackgroundColor(Color.WHITE);
}
 
源代码16 项目: Shield   文件: ViewUtils.java
public static int getTextViewWidth(TextView textView, String text) {
    textView.setText(text);
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(widthMeasureSpec, heightMeasureSpec);
    return textView.getMeasuredWidth();
}
 
源代码17 项目: remoteyourcam-usb   文件: PropertyDisplayer.java
public void setPropertyDesc(int[] values, String[] valuesStrings, Integer[] icons) {
    this.values = values;
    toggleButton.setEnabled(editable && values.length > 0);

    if (icons != null) {
        Drawable drawables[] = new Drawable[icons.length];
        for (int i = 0; i < icons.length; ++i) {
            if (icons[i] == null) {
                continue;
            }
            Drawable d = context.getResources().getDrawable(icons[i]);
            drawables[i] = d;
            if (biggestValueWidth == -1) {
                biggestValueWidth = d.getIntrinsicWidth();
                layoutListView(biggestValueWidth);
            }
        }
        adapter = new PropertyAdapter<Drawable>(context, R.layout.property_icon_list_item, drawables);
    } else {
        String longest = "";
        int longestLength = 0;
        for (String s : valuesStrings) {
            int len = s.length();
            if (len > longestLength) {
                longest = s;
                longestLength = len;
            }
        }
        TextView t = new TextView(context);
        // HACK HACK
        if (context.getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE)) {
            t.setTextAppearance(context, android.R.style.TextAppearance_Large);
        } else {
            t.setTextAppearance(context, android.R.style.TextAppearance_Medium);
        }
        t.setText(longest + " ");
        t.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        biggestValueWidth = t.getMeasuredWidth();
        layoutListView(biggestValueWidth);
        adapter = new PropertyAdapter<String>(context, R.layout.property_list_item, valuesStrings);
    }
    list.setAdapter(adapter);

    if (toggleButton.isChecked()) {
        if (values.length == 0) {
            listContainer.setVisibility(View.GONE);
        } else {
            listContainer.setVisibility(View.VISIBLE);
            updateCurrentPosition();
        }
    } else {
        dataChanged = true;
    }
}
 
源代码18 项目: remoteyourcam-usb   文件: PropertyDisplayer.java
public void setPropertyDesc(int[] values, String[] valuesStrings, Integer[] icons) {
    this.values = values;
    toggleButton.setEnabled(editable && values.length > 0);

    if (icons != null) {
        Drawable drawables[] = new Drawable[icons.length];
        for (int i = 0; i < icons.length; ++i) {
            if (icons[i] == null) {
                continue;
            }
            Drawable d = context.getResources().getDrawable(icons[i]);
            drawables[i] = d;
            if (biggestValueWidth == -1) {
                biggestValueWidth = d.getIntrinsicWidth();
                layoutListView(biggestValueWidth);
            }
        }
        adapter = new PropertyAdapter<Drawable>(context, R.layout.property_icon_list_item, drawables);
    } else {
        String longest = "";
        int longestLength = 0;
        for (String s : valuesStrings) {
            int len = s.length();
            if (len > longestLength) {
                longest = s;
                longestLength = len;
            }
        }
        TextView t = new TextView(context);
        // HACK HACK
        if (context.getResources().getConfiguration().isLayoutSizeAtLeast(Configuration.SCREENLAYOUT_SIZE_LARGE)) {
            t.setTextAppearance(context, android.R.style.TextAppearance_Large);
        } else {
            t.setTextAppearance(context, android.R.style.TextAppearance_Medium);
        }
        t.setText(longest + " ");
        t.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        biggestValueWidth = t.getMeasuredWidth();
        layoutListView(biggestValueWidth);
        adapter = new PropertyAdapter<String>(context, R.layout.property_list_item, valuesStrings);
    }
    list.setAdapter(adapter);

    if (toggleButton.isChecked()) {
        if (values.length == 0) {
            listContainer.setVisibility(View.GONE);
        } else {
            listContainer.setVisibility(View.VISIBLE);
            updateCurrentPosition();
        }
    } else {
        dataChanged = true;
    }
}
 
源代码19 项目: LemonHello4Android   文件: LemonHelloInfo.java
private int measureTextViewHeight(TextView textView, int viewWidth) {
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(_PST.dpToPx(viewWidth), View.MeasureSpec.AT_MOST);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    textView.measure(widthMeasureSpec, heightMeasureSpec);
    return _PST.pxToDp(textView.getMeasuredHeight());
}
 
源代码20 项目: NumberPicker   文件: ValuePicker.java
public static int getTextViewHeight(Context context, boolean isBig, float textSize, float textSizeSelected) {
    TextView textView = getTextView(context, isBig, textSize, textSizeSelected);
    textView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    return textView.getMeasuredHeight();
}
 
 方法所在类
 同类方法