【问题标题】:Android: draw xml Layout on canvasAndroid:在画布上绘制 xml 布局
【发布时间】:2017-01-23 09:57:12
【问题描述】:

我有一个自定义的PrintDocumentAdapter并自己绘制页面,所以我应该在页面画布上绘制xml布局:

private void drawPage(PdfDocument.Page page, List<Object> objects,
                          int pageNumber) {
   Canvas canvas = page.getCanvas();
   ...
}

我的 xml 中有一个 TableLayout。我希望表格宽度填满整个页面,但如果宽度大于页面宽度,则它超过页面,如果宽度小于页面宽度,则它小于页面。我同时使用了wrap_contentmatch_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>

现在结果:

【问题讨论】:

    标签: android canvas printing android-tablelayout android-inflate


    【解决方案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));
    ...
    }
    

    【讨论】:

      【解决方案2】:

      请尝试下面的 xml ,其中我在 TableRow 及其子文本视图中使用了 weightsum 和布局权重

      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>
      

      【讨论】:

      • 谢谢,但我不希望单元格的宽度相同。单元格具有不同的内容,因此它们的宽度应该不同
      猜你喜欢
      • 2012-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多