为什么滚动条没有出现在弹出窗口中?

时间:2015-10-18 15:59:48

标签: android scrollview android-tablelayout

我弹出窗口的布局如下所示。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

    <ScrollView 
        android:id="@+id/scroller"
        android:layout_marginTop="16dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/popup"
        android:fadingEdgeLength="5dip"
        android:scrollbars="vertical"
        android:fillViewport="true">

        <LinearLayout
            android:id="@+id/tracks"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="10dip"
            android:gravity="top|center"/>

    </ScrollView >

    <ImageView
        android:id="@+id/arrow_up"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow_up" />

    <ImageView
        android:id="@+id/arrow_down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/scroller"
        android:layout_marginTop="-4dip"
        android:src="@drawable/arrow_down" />

</RelativeLayout>

我在弹出窗口中显示的数据如下布局。

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
        android:id="@+id/pageTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:textColor="#000000"
        android:gravity="center"
            android:textSize="25sp"/>

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical">
            <View
                android:id="@+id/vwVerticalColourBar"
                android:layout_width="5dp"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:background="@color/grey"
                android:gravity="center_vertical" />
            <TextView
                android:id="@+id/tvVisitTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:gravity="center_vertical"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:textSize="16sp" />
            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:layout_marginBottom="8dp"
                android:layout_marginTop="8dp"
                android:background="#EDEDED" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="5dip" >

                <TextView
                    android:id="@+id/tvVisitStoreName"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:gravity="center_vertical"
                    android:textColor="#000000"
                    android:textSize="16sp" />

                <TextView
                    android:id="@+id/tvVisitStoreAddress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:gravity="center_vertical"
                    android:textColor="#333333"
                    android:textSize="12dp"
                    />
            </LinearLayout>

        </LinearLayout>

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <View
            android:layout_width="450dp"
            android:layout_height="1dp"
            android:background="@android:color/darker_gray"/>

    </TableRow>
</TableLayout>

我面临的问题是,滚动条没有出现。随着弹出窗口内的数据增加,弹出窗口大小也在增加。我想将弹出窗口高度限制为一些已经绝迹,如果弹出窗口内的数据增加,则应显示垂直滚动视图。请找到如下屏幕截图。

enter image description here

请让我知道我在哪里做错了。谢谢。

数据将在列表中,我将遍历列表并将其添加到布局中,如下所示。

for (int i = 0; i < currentDateVisitsAL.size(); i++) {
            container = mInflater.inflate(R.layout.cal_list_item_entry, mTrack,false);
            if(flag == true)
            {
                pageTitle = (TextView) container.findViewById(R.id.pageTitle);
                pageTitle.setText(day);
                flag =false;
            }
            View colourBar;
            TextView visitTimeTV;
            TextView visitStoreNameTV;
            TextView visitStoreAddressTV;
            colourBar = (View) container.findViewById(R.id.vwVerticalColourBar);
            visitTimeTV = (TextView) container.findViewById(R.id.tvVisitTime);
            visitStoreNameTV = (TextView) container.findViewById(R.id.tvVisitStoreName);
            visitStoreAddressTV = (TextView) container.findViewById(R.id.tvVisitStoreAddress);


            CalendarTabView.Reminder iItem = currentDateVisitsAL.get(i);

            int colour = mContext.getResources().getColor(R.color.white);
            boolean isTask;
            if (CalendarTabView.isTodo(iItem.getActivityHeader()))
                isTask = true;
            else isTask = false;
            if (isTask) {
                colour = mContext.getResources().getColor(R.color.cal_task_bg);
            } else if (iItem.getVisitStatus().contains("I1002")) {
                if (iItem.getActivityHeader().get__allDayVisit() != null && iItem.getActivityHeader().get__allDayVisit().equalsIgnoreCase("X"))
                    colour = Color.parseColor("#87CEFA");
                else
                    colour = mContext.getResources().getColor(R.color.visit_status_notstarted);
            } else if (iItem.getVisitStatus().contains("I1003")) {
                colour = mContext.getResources().getColor(R.color.visit_status_inprogress);
            } else if (iItem.getVisitStatus().contains("I1005")) {
                colour = mContext.getResources().getColor(R.color.visit_status_complete);
            } else if (iItem.getVisitStatus().contains("I1007")) {
                colour = mContext.getResources().getColor(R.color.native_calendar);
            } else {
                colour = mContext.getResources().getColor(R.color.visit_status_cancelled);
            }


            String fromTime = iItem.getFromTimeString();
            String toTime = iItem.getToTimeString();
            SimpleDateFormat sdf = new SimpleDateFormat("hhmm");
            try {
                Date fromTimeD = iItem.getActivityHeader().getPlanStartDateTime();
                Date toTimeD = iItem.getActivityHeader().getPlanEndDateTime();
                if (DateFormat.is24HourFormat(mContext)) {
                    sdf = new SimpleDateFormat("HH:mm");
                } else {
                    sdf = new SimpleDateFormat("hh:mm a");
                }

                fromTime = sdf.format(fromTimeD);
                toTime = sdf.format(toTimeD);
            } catch (Exception e) {
                MAFLogger.e("", "", e);
            }

            String visitStoreName = iItem.getCustName();// Customer name
            String visitStoreAddress; // Description
            if (isTask) {
                visitStoreAddress = mContext.getResources().getString(R.string.todo);
            } else {
                String actType = iItem.getActivityType();
                String actCatCode = iItem.getActivityHeader().get__ACT_CATG_CODE();
                visitStoreAddress = ActivityABO.getVisitTypeDescriptionByActAndCatType(actType, actCatCode);
            }
            colourBar.setBackgroundColor(colour);
            visitTimeTV.setText(fromTime + "-" + toTime);

            if (isTask && (visitStoreName == null || visitStoreName.isEmpty())) {
                visitStoreNameTV.setText(visitStoreAddress);
               visitStoreAddressTV.setText("");
            } else {
                visitStoreNameTV.setText(visitStoreName);
              visitStoreAddressTV.setText(visitStoreAddress);
            }

            mTrack.addView(container, mInsertPos);

            mChildPos++;
            mInsertPos++;

            final int finalI = i;
            container.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mItemClickListener != null) {
                        mItemClickListener.onItemClick(QuickAction.this, finalI);
                    }
                }
            });

        }
    }

mTrack是弹出布局中线性布局的ID。请找到如下代码。

public void setRootViewId(int id) {
        mRootView   = (ViewGroup) mInflater.inflate(id, null);
        mTrack      = (ViewGroup) mRootView.findViewById(R.id.tracks);

        mArrowDown  = (ImageView) mRootView.findViewById(R.id.arrow_down);
        mArrowUp    = (ImageView) mRootView.findViewById(R.id.arrow_up);

        mScroller   = (ScrollView) mRootView.findViewById(R.id.scroller);

        //This was previously defined on show() method, moved here to prevent force close that occured
        //when tapping fastly on a view to show quickaction dialog.
        //Thanx to zammbi (github.com/zammbi)
        mRootView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        setContentView(mRootView);
    }

工作部分:

   <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.sap.rex.ui.onboarding.MaxHeightScrollView
        android:id="@+id/scroller"
        android:layout_marginTop="16dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/popup"
        android:scrollbars="vertical"
        android:clickable="true"
        android:fadeScrollbars="false">

        <LinearLayout
            android:id="@+id/tracks"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dip"
            android:scrollbars="vertical"
            android:gravity="top|center"/>

    </com.sap.rex.ui.onboarding.MaxHeightScrollView >

    <ImageView
        android:id="@+id/arrow_up"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow_up" />

    <ImageView
        android:id="@+id/arrow_down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/scroller"
        android:layout_marginTop="-4dip"
        android:src="@drawable/arrow_down" />

</RelativeLayout>

ScrollView没有setMax height属性。我们需要编写自定义类。请在下面找到。

public class MaxHeightScrollView extends ScrollView {

    private int maxHeight;
    private final int defaultHeight = 800;

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

    public MaxHeightScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (!isInEditMode()) {
            init(context, attrs);
        }
    }

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

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public MaxHeightScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        if (!isInEditMode()) {
            init(context, attrs);
        }
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.MaxHeightScrollView);
            //200 is a defualt value
            maxHeight = styledAttrs.getDimensionPixelSize(R.styleable.MaxHeightScrollView_maxHeight, defaultHeight);

            styledAttrs.recycle();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(maxHeight, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

在attr.xml中添加以下内容。

 <declare-styleable name="MaxHeightScrollView">
  <attr name="maxHeight" format="integer" />
 </declare-styleable> 

1 个答案:

答案 0 :(得分:1)

将修复高度设置为pop-up's RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp">

<ScrollView 
    android:id="@+id/scroller"
    android:layout_marginTop="16dip"
      android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/popup"
    android:fadingEdgeLength="5dip"
    android:scrollbars="vertical"
    android:fillViewport="true">

    <LinearLayout
        android:id="@+id/tracks"
        android:orientation="vertical"
         android:layout_width="match_parent"
    android:layout_height="match_parent"
        android:layout_weight="1"
        android:padding="10dip"
        android:gravity="top|center"/>

</ScrollView >

<ImageView
    android:id="@+id/arrow_up"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/arrow_up" />

<ImageView
    android:id="@+id/arrow_down"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/scroller"
    android:layout_marginTop="-4dip"
    android:src="@drawable/arrow_down" />
</RelativeLayout>