我的活动中有一个TableLayout,它在Pixel API29(就像这样的https://ibb.co/T138m8G这样的ANdroi Pie)中可以很好地工作。
但是当我在较低的android版本(例如android 6.0)中尝试我的应用时,我的TableLayout会是这样。 https://ibb.co/ZK6VPcT
这是我的Java代码
FormReports.java
public class FormReports extends AppCompatActivity {
private Button btn, btnshare;
private LinearLayout llScroll;
private Bitmap bitmap;
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form_reports);
btn = findViewById(R.id.btn);
llScroll = findViewById(R.id.llScroll);
btnshare = (Button) findViewById(R.id.btn_share);
Toast.makeText(this, "Reports", Toast.LENGTH_SHORT).show();
FormReports context = this;
DatabaseHelper dataHelper = new DatabaseHelper(context);
TableLayout tableLayout = (TableLayout)
findViewById(R.id.tablelayout);
TableRow rowHeader = new TableRow(context);
rowHeader.setBackgroundColor(Color.parseColor("#c0c0c0"));
rowHeader.setLayoutParams(new
TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT));
String[] headerText = {"DATE", "COMPANY NAME", "DETAILS", "TIME IN",
"TIME OUT", "REMARKS"};
for (String c : headerText) {
TextView tv = new TextView(this);
tv.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
tv.setGravity(Gravity.CENTER);
tv.setTextSize(8);
tv.setPadding(10, 10, 10, 10);
tv.setText(c);
tv.setTextColor(ContextCompat.getColor(context, R.color.black));
rowHeader.addView(tv);
}
tableLayout.addView(rowHeader);
// Get data from sqlite database and add them to the table
// Open the database for reading
SQLiteDatabase db = dataHelper.getReadableDatabase();
// Start the transaction.
db.beginTransaction();
try {
String selectQuery = "SELECT * FROM " +
DatabaseHelper.TABLE_NAME_OUT;
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.getCount() > 0) {
while (cursor.moveToNext()) {
// Read columns data
String out_td =
cursor.getString(cursor.getColumnIndex("timei_date"));
String out_co =
cursor.getString(cursor.getColumnIndex("timei_cu"));
String out_st =
cursor.getString(cursor.getColumnIndex("timei_st"));
String out_in =
cursor.getString(cursor.getColumnIndex("timei_in"));
String out_out =
cursor.getString(cursor.getColumnIndex("timei_out"));
String out_le =
cursor.getString(cursor.getColumnIndex("timei_legend"));
// data rows
TableRow row = new TableRow(context);
row.setLayoutParams(new
TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT));
String[] colText = {out_td,
out_co,
out_st,
out_in,
out_out,
out_le};
for (String text : colText) {
TextView tv = new TextView(this);
tv.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
tv.setGravity(Gravity.CENTER);
tv.setTextSize(7);
tv.setPadding(3, 3, 3, 3);
tv.setText(text);
tv.setTextColor(ContextCompat.getColor(context,
R.color.black));
row.addView(tv);
}
tableLayout.addView(row);
}
}
db.setTransactionSuccessful();
} catch (SQLiteException e) {
e.printStackTrace();
} catch (Exception e) {
}
btnshare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
ShareFile();
} else {
ShareFile();
}
}
});
btn.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View v) {
Log.d("size"," "+llScroll.getWidth() +"
"+llScroll.getWidth());
bitmap = loadBitmapFromView(llScroll, llScroll.getWidth(),
llScroll.getHeight());
createPdf();
}
});
}
public static Bitmap loadBitmapFromView(View v, int width, int height) {
Bitmap b = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private void createPdf(){
WindowManager wm = (WindowManager)
getSystemService(Context.WINDOW_SERVICE);
// Display display = wm.getDefaultDisplay();
DisplayMetrics displaymetrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
float hight = displaymetrics.heightPixels ;
float width = displaymetrics.widthPixels ;
int convertHighet = (int) hight, convertWidth = (int) width;
// Resources mResources = getResources();
// Bitmap bitmap = BitmapFactory.decodeResource(mResources,
R.drawable.screenshot);
PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new
PdfDocument.PageInfo.Builder(convertWidth, convertHighet, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
Canvas canvas = page.getCanvas();
Paint paint = new Paint();
canvas.drawPaint(paint);
bitmap = Bitmap.createScaledBitmap(bitmap, convertWidth,
convertHighet, true);
paint.setColor(Color.BLUE);
canvas.drawBitmap(bitmap, 0, 0 , null);
document.finishPage(page);
// write the document content
String targetPdf = "/sdcard/timerecords.pdf";
File filePath;
filePath = new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Something wrong: " + e.toString(),
Toast.LENGTH_LONG).show();
}
// close the document
document.close();
Toast.makeText(this, "PTAF CREATED", Toast.LENGTH_SHORT).show();
}
private void ShareGif() {
}
protected void ShareFile() {
File exportDir = new File(Environment.getExternalStorageDirectory(),
"/");
String fileName = "timerecords.pdf";
File sharingGifFile = new File(exportDir, fileName);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("application/pdf");
// Uri uri = Uri.fromFile(sharingGifFile);
Uri uri = FileProvider.getUriForFile(FormReports.this,
"com.example.serviceapplication.provider", sharingGifFile);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share Time
Records"));
}
}
这是我的XML代码
activity_form_reports.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:fillViewport="false"
tools:context=".FormTimeIn"
android:background="#ffffff">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/llScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableLayout
android:id= "@+id/tablelayout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:shrinkColumns="0"
app:tabTextColor="@color/black">
<TextView
android:layout_width="10dp"
android:layout_weight="1"
android:text="PAYROLL AND TIMEKEEPING AUTHORIZATION FORM"
android:textAlignment="center"
android:layout_gravity="center"
android:textColor="@color/black"
android:layout_marginTop="15dp"/>
<TableRow>
<TextView
android:layout_width="266dp"
android:text="Name:"
android:textSize="8dp"
android:textColor="@color/black"/>
<TextView android:text="Department:"
android:layout_width="50dp"
android:layout_column="0"
android:layout_weight="1"
android:layout_marginLeft="40dp"
android:textSize="8dp"
android:textColor="@color/black"/>
<TextView android:text="Cutt off Period:"
android:layout_width="match_parent"
android:layout_column="0"
android:layout_weight="1"
android:textSize="8dp"
android:textColor="@color/black"/>
</TableRow>
<TableRow
android:id="@+id/tablerow1">
<TextView
android:layout_width="314dp"
android:layout_column="0"
android:layout_weight="1"
/>
<TextView
android:layout_width="75dp"
android:layout_column="0"
android:layout_weight="1"
android:text="Service Department"
android:textSize="10dp"
android:textColor="@color/black"/>
<TextView
android:layout_width="match_parent"
android:layout_column="0"
android:layout_weight="1"
android:textColor="@color/black"/>
</TableRow>
<TableRow>
</TableRow>
</TableLayout>
</HorizontalScrollView>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prepared By:"
android:layout_columnWeight="0"
android:width="70dp"
android:textSize="10dp"
android:textColor="@color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Checked By:"
android:layout_columnWeight="0"
android:width="70dp"
android:textSize="10dp"
android:textColor="@color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Approved By:"
android:layout_columnWeight="0"
android:width="70dp"
android:textSize="10dp"
android:textColor="@color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Received By:"
android:layout_columnWeight="0"
android:width="150dp"
android:textSize="10dp"
android:textColor="@color/black"/>
</GridLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="4"
android:layout_marginTop="10dp"
android:textColor="@color/black">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Employees Signature"
android:layout_columnWeight="0"
android:width="70dp"
android:textSize="5dp"
android:textColor="@color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Immediate Supervisor's Signature"
android:layout_columnWeight="0"
android:width="70dp"
android:textSize="5dp"
android:textColor="@color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Department Manager's Signature"
android:layout_columnWeight="0"
android:width="70dp"
android:textSize="5dp"
android:textColor="@color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HRD Manager/Staff's Signature"
android:layout_columnWeight="0"
android:width="150dp"
android:textSize="5dp"
android:textColor="@color/black"/>
</GridLayout>
<View
android:layout_width="wrap_content"
android:layout_height="2dp"
android:background="@color/black"
/>
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="2"
android:rowCount="1"
android:layout_gravity="center"
android:layout_marginTop="400dp">
<Button
android:id="@+id/btn"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="GENERATE PTAF"
android:layout_columnWeight="0"
android:background="@drawable/custom_button3"
android:textColor="@color/white"/>
<Button
android:id="@+id/btn_share"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="SEND PTAF"
android:layout_columnWeight="1"
android:width="150dp"
android:background="@drawable/custom_button3"
android:textColor="@color/white"/>
</GridLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
我只想使输出与我在任何尺寸的手机中显示给您的第一张图片相同,非常感谢。
只需像这样将您的layout_widths设置为match_parent:
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/llScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableLayout
android:id= "@+id/tablelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:shrinkColumns="0"
app:tabTextColor="@color/black">