0

我有一个很奇怪的问题;我希望有人知道这是什么。我在可滚动视图中构建了一个表格,带有 2px 厚的单元格边框。在平板电脑(三星 Galaxy 10.1)上它是完美的,而在 Galaxy Tab 2 上它“停止”仅在滚动视图的可见部分绘制边框。我附上两张图片以显示差异 三星 Galaxy Tab 10.1

三星 Galaxy Tab 2(向右滚动一点)

这是java部分的代码:

  @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            super.onCreateView(inflater, container, savedInstanceState);
            scrollView1=(HorizontalScrollView) firstView.findViewById(R.id.horizontalScrollView1);
            //position the scroller to the present time
            scrollView1.post(new Runnable() { 
                public void run() { 
                    if((Calendar.getInstance().get(Calendar.HOUR_OF_DAY)>3))
                     scrollView1.scrollTo(CELL_WIDTH*((Calendar.getInstance().get(Calendar.HOUR_OF_DAY))-3), 0);
                } 
        });

             headerPaint = new Paint();
             headerPaint.setStyle(Paint.Style.STROKE);
             headerPaint.setColor(Color.GRAY);
             headerPaint.setStrokeWidth(2);

             cellPaint = new Paint();
             cellPaint.setStyle(Paint.Style.STROKE);
             cellPaint.setColor(Color.GRAY);
             cellPaint.setStrokeWidth(2);

            textViewData.setText(formatter.format(dateToShow.getTime()));
            imageButtonDateAfter.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Calendar oggi = Calendar.getInstance();
                    oggi.set(Calendar.HOUR_OF_DAY, 23);
                    oggi.set(Calendar.MINUTE, 59);
                    dateToShow.add(Calendar.DAY_OF_MONTH, 1);   
                }
            });
            loadElementiInTable();
            return firstView;
        }

//for space reasons i show you only the first row code (the hours of the day)
 private void loadElementiInTable() {
        tableLayoutElementi.removeAllViews();
        tableLayoutHeader.removeAllViews();
        ArrayList<Integer> orari = new ArrayList<Integer>();
        orari = loadListOrari();
        listPasti = new ArrayList<Pasto>();
        listGlicemie = new ArrayList<Glicemia>();
        listBoli = new ArrayList<Administration>();
        TableRow hoursRow=new TableRow(getActivity().getApplicationContext());
        TableRow hoursHeaderRow=new TableRow(getActivity().getApplicationContext());
        TableRow hoursSepRow = new TableRow(getActivity().getApplicationContext());
        TextView legendanew=new TextView(getActivity().getApplicationContext());

        legendanew.setText("Campo");
        legendanew.setWidth(HEADER_WIDTH);
        legendanew.setGravity(Gravity.CENTER);
        hoursHeaderRow.addView(legendanew);

        for (final Integer j : orari) {
        TextView ora1=new TextView(getActivity().getApplicationContext()){
            @Override
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                Rect rect = new Rect();
                Paint paint = new Paint();
                paint.setStyle(Paint.Style.STROKE);
                paint.setColor(Color.GRAY);
                paint.setStrokeWidth(2);
                getLocalVisibleRect(rect);
                canvas.drawRect(rect, paint);       
            }

        };
        ora1.setTextColor(Color.DKGRAY);
        ora1.setGravity(Gravity.CENTER);
        TextView sepOra=new TextView(getActivity().getApplicationContext());
        ora1.setText(j+":00");
        if(j==Calendar.getInstance().get(Calendar.HOUR_OF_DAY)){
            ora1.setBackgroundColor(Color.YELLOW);
        }
        ora1.setWidth(CELL_WIDTH);
        hoursRow.addView(ora1);
        //hoursRow.addView(sepOra);
        }

        TableRow glicemiaRow = new TableRow(getActivity().getApplicationContext());
        TableRow glicemiaHeaderRow = new TableRow(getActivity().getApplicationContext());
        TextView glicemiaLabel=new TextView(getActivity().getApplicationContext()){
            @Override
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                Rect rect = new Rect();
                getLocalVisibleRect(rect);
                canvas.drawRect(rect, headerPaint);       
            }

        };
        glicemiaLabel.setText("Glicemia");
        formatAsHeader(glicemiaLabel);  
        glicemiaHeaderRow.addView(glicemiaLabel);
        TextView glicemiaTuttoIlGiorno=new TextView(getActivity().getApplicationContext()){
            @Override
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                Rect rect = new Rect();
                getLocalVisibleRect(rect);
                canvas.drawRect(rect, cellPaint);       
            }
        };
        if(showTuttoIlGiorno){
        glicemiaTuttoIlGiorno.setText(EMPTY_CELL);
        formatAsTuttoIlGiorno(glicemiaTuttoIlGiorno);
        glicemiaHeaderRow.addView(glicemiaTuttoIlGiorno);
        }

        tableLayoutElementi.setStretchAllColumns(true);
        legendanew.setWidth(100);
        noteRow.setBaselineAligned(false);
        noteHeaderRow.setBaselineAligned(false);
        tableLayoutElementi.addView(noteRow, 0, new TableLayout.LayoutParams(100, CELL_HEIGHT));
        tableLayoutHeader.addView(noteHeaderRow, 0, new TableLayout.LayoutParams(100, CELL_HEIGHT));
        boloRow.setBaselineAligned(false);
        boloHeaderRow.setBaselineAligned(false);
        tableLayoutElementi.addView(boloRow, 0, new TableLayout.LayoutParams(100, CELL_HEIGHT));
        tableLayoutHeader.addView(boloHeaderRow, 0, new TableLayout.LayoutParams(100, CELL_HEIGHT));
        pastoRow.setBaselineAligned(false);
        pastoHeaderRow.setBaselineAligned(false);
        tableLayoutElementi.addView(pastoRow, 0, new TableLayout.LayoutParams(100, CELL_HEIGHT));
        tableLayoutHeader.addView(pastoHeaderRow, 0, new TableLayout.LayoutParams(100, CELL_HEIGHT));
        glicemiaRow.setBaselineAligned(false);
        glicemiaHeaderRow.setBaselineAligned(false);
        tableLayoutElementi.addView(glicemiaRow, 0, new TableLayout.LayoutParams(100, CELL_HEIGHT));
        tableLayoutHeader.addView(glicemiaHeaderRow, 0, new TableLayout.LayoutParams(100, CELL_HEIGHT));
        tableLayoutHeader.addView(hoursHeaderRow, 0, new TableLayout.LayoutParams(100, heightColonna));
        tableLayoutElementi.addView(hoursRow, 0, new TableLayout.LayoutParams(100, heightColonna));
    }
    }

xml 文件(仅表格部分):

 <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="10" >

        <HorizontalScrollView
            android:id="@+id/horizontalScrollView1"
            android:layout_width="1080dp"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

            <TableLayout
                android:id="@+id/tableLayoutElementi"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@color/white" >
            </TableLayout>

 </LinearLayout>

            </HorizontalScrollView>

        <LinearLayout
            android:id="@+id/headerleft"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TableLayout
                android:id="@+id/tableLayoutHeader"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >

                <TableRow
                    android:id="@+id/tableRow1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView" />

                </TableRow>

                <TableRow
                    android:id="@+id/tableRow2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >

                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView" />

                </TableRow>

                <TableRow
                    android:id="@+id/tableRow3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >
                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView" />
                </TableRow>
                <TableRow
                    android:id="@+id/tableRow4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" >
                </TableRow>
            </TableLayout>
        </LinearLayout>
    </RelativeLayout>
4

1 回答 1

0

我不明白为什么会这样,但我解决了删除这些 textview 声明:

TextView cell = new TextView(getActivity().getApplicationContext()) {
                @Override
                protected void onDraw(Canvas canvas) {
                    super.onDraw(canvas);
                    Rect rect = new Rect();
                    getLocalVisibleRect(rect);
                    canvas.drawRect(rect, cellPaint);       
                }
            };

并把它改为:

TextView cell = new TextView(getActivity().getApplicationContext());
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.borded_cell));

其中 borded_cell 是我放入 res/drawable 的 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
   <solid android:color="#ffffff" />
   <stroke android:width="1dip" android:color="#4fa5d5"/>
</shape>
于 2013-04-23T14:33:57.853 回答