MaxHeightScroolView 的一个问题

描述

一个控制最大高度的scrollVIew,也就是 内容少的时候 wrap content, 多的时候有个最大高度
存在的问题是没有 起作用

xml code

<com.common.components.MaxHeightScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/notification_dialog_bg"
        android:fadingEdge="none"
        android:overScrollMode="never"
        android:scrollbars="none">

        <!--怎么直接这样写有问题-->

        <TextView
            android:id="@+id/content_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="25dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="10dp"
            android:text=""
            android:textColor="#333333"
            android:textSize="13sp" />
    </comcommon.components.MaxHeightScrollView>

MaxHeightScrollView


import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.widget.ScrollView;

/**
 */
public class MaxHeightScrollView extends ScrollView {
    private Context mContext;

    public MaxHeightScrollView(Context context) {
        super(context);
        init(context);
    }

    public MaxHeightScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public MaxHeightScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        try {
            Display display = ((Activity) mContext).getWindowManager().getDefaultDisplay();
            DisplayMetrics d = new DisplayMetrics();
            display.getMetrics(d);
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(d.heightPixels / 3, MeasureSpec.AT_MOST);

        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

这是我 塞入文字的地方

    private void init() {
        setContentView(R.layout.dialog_notification_user);
        setCanceledOnTouchOutside(false);
        showContentTV = (TextView) findViewById(R.id.content_tv);
    }
    public NotificationDialogInUser showWhat(String str) {
        if (showContentTV != null) {
            showContentTV.setText(str + str + str + str + str + str + str +str + str + str + str);
        }
        return this;
    }

目前效果是这样

image.png

待解决 ???

断点 MaxHeightScrollView 的 measure 没走。

还有怀疑是 background 的 .9 图的事儿,换成一般的颜色也不对,所以排除.9图的问题

解决

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        init();
        showContentTV.setText(contenStr);
    }

    private void init() {

        LayoutInflater inflater = LayoutInflater.from(mContext);
        View view = inflater.inflate(R.layout.dialog_notification_user, null);
        setContentView(view);
        //setContentView(R.layout.dialog_notification_user);
        //**************************************
        //setContentView(R.layout.dialog_notification_user); 有问题, MaxHeightScrollView 失效,onMeasure 未能触发
        // 所以搞清楚他们之间的关系
        // 1. View view = inflater.inflate(R.layout.dialog_notification_user, null);
        // 2. RV  中的 holder new DescHolder(mInflater.inflate(R.layout.search_attribute_desc_item, parent, false));
        //**************************************
        setCanceledOnTouchOutside(false);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = ScreenUtil.WIDTH - 200;
        //lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        dialogWindow.setAttributes(lp);
        dialogWindow.setGravity(Gravity.CENTER);
        showContentTV = (TextView) findViewById(R.id.content_tv);
        close = (ImageView) findViewById(R.id.close_btn);

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,242评论 25 708
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 2,333评论 0 11
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    wzhiq896阅读 1,799评论 0 2
  •   Android中的View在Android的知识体系中扮演着重要的角色。简单来说,View就是Android在...
    忆念成风阅读 2,265评论 0 14
  • 1,查看一个静态库信息,XXX为静态库名称 lipo -info XXX 2,合并两个静态库 lipo -crea...
    三十一_iOS阅读 140评论 0 0