2017-01-23 174 views
0

XML布局我有一个自定义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> 

现在结果:
results

回答

0

请尝试以下XML,其中我的TableRow及其子textviews使用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> 
+0

谢谢,但我不希望单元格的宽度相同。细胞有不同的内容,所以它们的宽度应该不同 – Mneckoee

0

我不该伸的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)); 
... 
}