Java 带有文本和按钮的编程表行
我有这样一个布局,它正确地呈现了我的视图,我试图将它与Java端相匹配,以便能够动态地构建行,但我一直未能实现这一点Java 带有文本和按钮的编程表行,java,android,Java,Android,我有这样一个布局,它正确地呈现了我的视图,我试图将它与Java端相匹配,以便能够动态地构建行,但我一直未能实现这一点 <TableLayout> android:layout_width="match_parent" android:layout_height="wrap_content" <TableRow android:layout_width="match_
<TableLayout>
android:layout_width="match_parent"
android:layout_height="wrap_content"
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="200dp"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextViewaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaws" />
</LinearLayout>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</TableRow>
</TableLayout>
android:layout\u width=“匹配父项”
android:layout\u height=“包装内容”
我确实想在代码中实现它,我已经尝试了很多次,这是我最好的尝试
public static void makeTablePostOrders(Context ct, TableLayout tableLayout_PostOrders,
List<String> postOrders)
{
TableRow.LayoutParams tableRowParams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(200,
LinearLayout.LayoutParams.MATCH_PARENT,1);
//randoms
for(int i = 0; i < postOrders.size()/2 ; i++)
{
//make tableRow
TableRow tableRow = new TableRow(ct);
// make LinearLayout
LinearLayout layout = new LinearLayout(ct);
layout.setLayoutParams(linearLayoutParams);
layout.setOrientation(LinearLayout.VERTICAL);
//make TextView
TextView tv = new TextView(ct);
tv.setText(postOrders.get(i));
tv.setTextColor(Color.BLACK);
// make button
// the button should send us to next view
Button bt_View = new Button(ct);
bt_View.setText("Select");
//adding views
layout.addView(tv,-2,-2);
tableRow.addView(layout);
tableRow.addView(bt_View,-2,-2);
tableLayout_PostOrders.addView(tableRow,i+1);
}
}
公共静态void makeTablePostOrders(上下文ct、TableLayout、TableLayout\u PostOrders、,
(邮购清单)
{
TableRow.LayoutParams tableRowParams=新建TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_内容,
TableRow.LayoutParams.WRAP_内容);
LinearLayout.LayoutParams linearLayoutParams=新的LinearLayout.LayoutParams(200,
LinearLayout.LayoutParams.MATCH_PARENT,1);
//随机化
对于(int i=0;i
我想要的是用文本和按钮创建表行,但是文本不应该通过屏幕,如果需要,总是垂直向下
这是一张图片,每行有一张
根据您的要求实施设计的正确方法是listview或recyclerview。 因此,最好使用Listview/RecycleView而不是动态生成列表行 列表行的xml如下所示:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/myTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".65"
android:text="You text will come here" />
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button" />
</LinearLayout>
线性布局的android:layout_width=“200dp”
的任何原因试图不让它通过屏幕,我是新加入的。请分享预期输出的快照以及您从该代码获得的输出。按要求添加图片@SweetWisherツ伟大的我将很快发布解决方案。谢谢,我将研究这些,XML是完美的