安卓:在画布上绘制XML布局

3
我有一个定制的PrintDocumentAdapter,并且自己绘制页面,所以我应该在页面画布上绘制xml布局:
private void drawPage(PdfDocument.Page page, List<Object> objects,
                          int pageNumber) {
   Canvas canvas = page.getCanvas();
   ...
}

我的XML中有一个TableLayout。我想让表格的宽度填满整个页面,但如果宽度大于页面宽度,则会超出页面,如果宽度小于页面宽度,则会小于页面。我尝试过使用wrap_content和match_parent,但都没有起作用。我甚至尝试了固定宽度和高度。
private void drawPage(PdfDocument.Page page, List<Payment> payments,
                          int pageNumber) {
        Canvas canvas = page.getCanvas();
        PdfDocument.PageInfo pageInfo = page.getInfo();

        canvas.save();
        canvas.translate(leftMargin , topMargin);

        FrameLayout frameLayout = new FrameLayout(context);
        frameLayout.setLayoutParams(new ViewGroup.LayoutParams(200, 200));
        LayoutInflater li = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = li.inflate(R.layout.payments_table_layout, null);
        v.setLayoutParams(new   
FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        TableLayout tableLayout = (TableLayout) v.findViewById(R.id.payments_table_layout);

        for(int i= 0 ; i< objects.size(); ++i){
//... some code
        }

        frameLayout.addView(v);
        frameLayout.measure(200 , 200);
        frameLayout.layout(100, 100, 100, 100);
        frameLayout.draw(canvas);
        canvas.restore();
    } 

R.layout.payments_table_layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="50dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="some text"
            style="@style/CustomFont.Wave"/>
    </FrameLayout>
    <TableLayout
        android:id="@+id/payments_table_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:stretchColumns="*"
        android:layout_weight="1">
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="#ccc">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="some text"
                android:layout_gravity="center"
                android:background="@drawable/table_border"
                android:padding="4dp"
                />
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="some text"
                android:layout_gravity="center"
                android:background="@drawable/table_border"
                android:padding="4dp"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="some text"
                android:layout_gravity="center"
                android:background="@drawable/table_border"
                android:padding="4dp"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="some text"
                android:layout_gravity="center"
                android:background="@drawable/table_border"
                android:padding="4dp"/>
        </TableRow>
    </TableLayout>
</LinearLayout>

现在的结果是:
results
2个回答

1

我不应该拉伸TableLayout的所有列,所以我将这个属性改为:

android:stretchColumns="1,3,7"  
android:shrinkColumns="5"

并将布局宽度设置为页面的宽度,像这样(我们应该关注尺寸单位):在我的customPrintDocumentAdapter中。

@Override
    public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
        myPdfDocument = new PrintedPdfDocument(context, newAttributes);

        pageHeight =
                newAttributes.getMediaSize().getHeightMils()/1000 * 72;
        pageWidth =
                newAttributes.getMediaSize().getWidthMils()/1000 * 72;

        ...
    }

我的新的drawPage方法:
private void drawPage(PdfDocument.Page page, List<Payment> payments,
                          int pageNumber) {
... 
frameLayout.setLayoutParams(new ViewGroup.LayoutParams(pageWidth, pageHeight));
...
}

0
请尝试以下XML代码,在其中我在TableRow和其子TextView中使用了weightsum和layout weight。

android:weightSum="4"

android:layout_width="0dp"
android:layout_weight="1"
<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center">
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="50dp">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="some text"
               />
        </FrameLayout>
        <TableLayout
            android:id="@+id/payments_table_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:stretchColumns="*"
            android:layout_weight="1">
            <TableRow
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:weightSum="4"
                android:layout_gravity="center_horizontal"
                android:background="#ccc">
                <TextView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="some text"
                    android:layout_gravity="center"
                    android:background="@color/colorAccent"
                    android:padding="4dp"
                    />
                <TextView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="some text"
                    android:layout_gravity="center"
                    android:background="@color/colorAccent"
                    android:padding="4dp"/>
                <TextView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="some text"
                    android:layout_gravity="center"
                    android:background="@color/colorAccent"
                    android:padding="4dp"/>
                <TextView
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:text="some text"
                    android:layout_gravity="center"
                    android:background="@color/colorAccent"
                    android:padding="4dp"/>
            </TableRow>
        </TableLayout>
    </LinearLayout>

谢谢,但我不想让单元格具有相同的宽度。由于单元格内容不同,因此它们的宽度应该是不同的。 - Mneckoee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接