![](/img/trans.png)
[英]Android: How to equally divide the table layout height to table rows in programmatically
[英]Trying to get table rows equal height in table layout
我正在使用 TableLayout,其中每個表格行將由三個單元格組成,每個單元格將顯示一個圖像。 我希望每個圖像視圖/單元格的高度和寬度相等。 問題是,默認情況下,tablerow 高度會包裹到具有最大高度的 imageview/cell 的大小,因此行的高度不相等。
TableLayout tableLayout = (TableLayout) getActivity().findViewById(R.id.tableLayout);
TableRow.LayoutParams lp = new TableRow.LayoutParams(tableLayout.getWidth()/3, ViewGroup.LayoutParams.WRAP_CONTENT, 0.3f);
TableRow tableRow = new TableRow(getActivity().getApplicationContext());
lp.setMargins(1,1,1,1);
tableRow.setLayoutParams(lp);
imageView.setImageBitmap(b);
imageView.setLayoutParams(lp);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
tableRow.addView(imageView);
tableLayout.addView(tableRow);
這是xml
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*"
android:weightSum="1"
app:layout_constrainedHeight="false"
app:layout_constraintHeight_percent="0.5"
app:layout_constraintWidth_percent="1">
</TableLayout>
您可以使用 2 種方式來解決您的問題
一:
在您的 xml 中設置類似此代碼的內容
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="200dp"
android:layout_height="200dp"
app:layout_constrainedHeight="false"
app:layout_constraintHeight_percent="0.5"
app:layout_constraintWidth_percent="1">
</TableLayout>
通過這種方式,您可以對布局使用您想要的寬度和高度,例如 Instagram。
二:將相同大小的圖像發送到您的應用程序或將其調整為您想要的大小
我更喜歡兩種方式在一起。
在評論中更新我
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.