当我使用 XML 静态渲染它时,我试图渲染一个看起来不错的视图,但是当我在 Java 中复制相同的结构时它不会。
<TableLayout
android:id="@+id/trends_table_body"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@drawable/border">
<TableRow android:weightSum="100">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="5"
android:background="@drawable/border"
android:text="5"
android:textAlignment="center"
android:textSize="@dimen/bootstrap_h4_text_size" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="30"
android:background="@drawable/border"
android:orientation="horizontal">
<com.beardedhen.androidbootstrap.AwesomeTextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rotation="135"
android:text=""
app:bootstrapBrand="danger"
app:fontAwesomeIcon="fa_arrow_up" />
</LinearLayout>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="25"
android:background="@drawable/border"
android:text="BTC/USDT"
android:textAlignment="center"
android:textSize="@dimen/bootstrap_h4_text_size" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="26"
android:background="@drawable/border"
android:text="22%"
android:textAlignment="center"
android:textSize="@dimen/bootstrap_h4_text_size" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="14"
android:background="@drawable/border"
android:orientation="horizontal">
<com.beardedhen.androidbootstrap.BootstrapButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="More"
app:bootstrapBrand="primary"
app:bootstrapSize="sm"
app:buttonMode="regular"
app:roundedCorners="true"
app:showOutline="false" />
</LinearLayout>
</TableRow>
</TableLayout>
爪哇:
private TableRow createStaticEvent(final EventDto event) {
// create LayoutParams for image and button's Linear Layouts (elements goes from 0 to 4)
LinearLayout.LayoutParams wrapContent1 = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT);
wrapContent1.weight = 30f;
LinearLayout.LayoutParams wrapContent4 = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.MATCH_PARENT);
wrapContent4.weight = 14f;
// container
TableRow eventView = new TableRow(TrendsActivity.this);
eventView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
eventView.setWeightSum(100);
// Row Index
TextView rowIndex = new AppCompatTextView(TrendsActivity.this);
TableRow.LayoutParams rowIndex_lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 5);
rowIndex.setLayoutParams(rowIndex_lp);
rowIndex.setGravity(Gravity.CENTER_HORIZONTAL);
rowIndex.setBackgroundResource(R.drawable.border);
rowIndex.setText("1"); // TODO - needs to be dynamically
rowIndex.setTextSize(R.dimen.bootstrap_h4_text_size);
// arrow (in LinearLayout - LayoutParams1)
AwesomeTextView expectedTo = new AwesomeTextView(TrendsActivity.this);
TableRow.LayoutParams expectedTo_lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
expectedTo.setLayoutParams(expectedTo_lp);
expectedTo.setRotation(45f); // TODO - needs to be dynamically
expectedTo.setText("");
expectedTo.setBootstrapBrand(DefaultBootstrapBrand.SUCCESS);
expectedTo.setFontAwesomeIcon(FontAwesome.FA_ARROW_UP);
LinearLayout expectedToContainer = new LinearLayout(TrendsActivity.this);
expectedToContainer.setLayoutParams(wrapContent1);
expectedToContainer.addView(expectedTo);
// asset
TextView asset = new TextView(TrendsActivity.this);
TableRow.LayoutParams asset_lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 25f);
asset.setLayoutParams(asset_lp);
asset.setBackgroundResource(R.drawable.border);
asset.setText("BTC/USDT"); // TODO - needs to be dynamically
asset.setTextSize(R.dimen.bootstrap_h4_text_size);
asset.setGravity(Gravity.CENTER_HORIZONTAL);
// probability
TextView probability = new TextView(TrendsActivity.this);
TableRow.LayoutParams probability_lp = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 26f);
probability.setLayoutParams(probability_lp);
probability.setBackgroundResource(R.drawable.border);
probability.setText("10%"); // TODO - needs to be dynamically
probability.setTextSize(R.dimen.bootstrap_h4_text_size);
probability.setGravity(Gravity.CENTER_HORIZONTAL);
// moreButton
BootstrapButton moreButton = new BootstrapButton(TrendsActivity.this);
TableRow.LayoutParams moreButton_lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
moreButton.setLayoutParams(moreButton_lp);
moreButton.setBootstrapBrand(DefaultBootstrapBrand.PRIMARY);
moreButton.setBootstrapSize(DefaultBootstrapSize.SM);
moreButton.setButtonMode(ButtonMode.REGULAR);
moreButton.setRounded(true);
moreButton.setShowOutline(false);
moreButton.setText("More");
moreButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(TrendsActivity.this, TrendActivity.class);
intent.putExtra("id", "5e9b4f6a48cdb0002ab99f78"); // TODO - needs to be dynamically
TrendsActivity.this.startActivity(intent);
}
});
LinearLayout moreButtonContainer = new LinearLayout(TrendsActivity.this);
moreButtonContainer.setLayoutParams(wrapContent4);
moreButtonContainer.addView(moreButton);
eventView.addView(rowIndex);
eventView.addView(expectedToContainer);
eventView.addView(asset);
eventView.addView(probability);
eventView.addView(moreButtonContainer);
return eventView;
}
布局检查器显示 XML 渲染视图的高度为 85: XML 渲染视图
布局检查器显示 Java 渲染视图的高度为 0: Java 渲染视图
即使我手动将高度设置为 85 ( eventView.setMinimumHeight(85);
),它也会显示一个空视图。里面的元素以高度 0 渲染。