我尝试创建一个包含图像的cardView,在该图像旁边,我想添加两行文本(彼此垂直堆叠)。下面是我为此编写的代码(该函数返回cardView)。但是,在我的cardView中什么也没出现。如果我删除linearLayout和textViews,则会得到一张显示imageView的卡片。因此,我假设我在textViews上设置linearLayout或将linearLayout添加到tableRow的方式做错了。
private fun constructCardView(header: String, info: String) : CardView {
val cardView = CardView(this)
cardView.setPaddingRelative(5,0,0,0)
cardView.radius = 10F
val tableLayout = TableLayout(this)
val layoutParams = TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT, 1.0f)
tableLayout.layoutParams = layoutParams
val tableRow = TableRow(this)
val tableRowParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT, 1.0f)
tableRow.layoutParams = tableRowParams
val imageView = ImageView(this)
imageView.setImageResource(R.drawable.ic_restaurant_black_24dp)
imageView.minimumHeight = 10
imageView.minimumWidth = 10
val linearLayout = LinearLayout(this)
val linearLayoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
linearLayoutParams.setMargins(3,3,3,3)
linearLayout.layoutParams = linearLayoutParams
linearLayout.orientation = LinearLayout.VERTICAL
val textViewHeader = TextView(this)
val textViewHeaderLayoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
textViewHeaderLayoutParams.setMargins(0, 0, 5, 0)
textViewHeader.layoutParams = textViewHeaderLayoutParams
textViewHeader.text = header
textViewHeader.textSize = 20F
val textViewInfo = TextView(this)
val textViewInfoLayoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)
textViewInfoLayoutParams.setMargins(5, 0, 5, 0)
textViewInfo.text = info
textViewInfo.textSize = 12F
textViewInfo.layoutParams = textViewInfoLayoutParams
linearLayout.addView(textViewHeader)
linearLayout.addView(textViewInfo)
tableRow.addView(imageView)
tableRow.addView(linearLayout)
tableLayout.addView(tableRow)
cardView.addView(tableLayout)
cardView.requestLayout()
return cardView
}
任何帮助将不胜感激。
答案 0 :(得分:0)
如果使用xml是一种选择,则只是提供一种选择,因为我认为这样做可以节省很多行代码来完成相同的工作。
您可能想使用LinearLayout
而不是Tablelayout
来垂直或水平堆叠事物,这是一种更通用的方法。例如,我使用了layout_weight
属性来定义如何按百分比拆分行。
在layouts
文件夹中创建一个xml文件:
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="200dp"
android:layout_height="200dp"
card_view:cardCornerRadius="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView android:layout_width="0dp"
android:layout_weight="0.4"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v7.widget.CardView>
然后输入代码:
private fun constructCardView(header: String, info: String) : CardView {
val cardView = LayoutInflater.from(this).inflate(R.layout.cardview, parent, false)
cardView.header.text = header
cardView.info.text = info
return cardView as CardView
}