java以编程方式将TextView添加到现有XML文件中
我的主屏幕显示来自用户输入的信息
现在,除了左上角的“Kool”文本视图之外,这个屏幕上的所有内容都是使用XML添加的。现在我想要一种从用户那里获取信息并将其添加到第一行信息下面的方法
我已经整理好了获得用户输入的屏幕,问题实际上是布局是否完美。现在我以编程方式添加了“Kool”TextView,但布局是错误的
如何使“Kool”文本视图位于日期“20/08/15”下方。每次使用setHeight方法时,都会使TextView不可见
代码:
public class AnalysisPage extends Activity {
Button savebutton,cancelbutton;
EditText dateinput,weightinput,repsinput;
int dd,mm,yy;
private TextView texty = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dumbbellpress);
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
case R.id.addinfo:
Dialog dialog = new Dialog(AnalysisPage.this);
dialog.setTitle(" Enter your STATS brah");
dialog.setContentView(R.layout.datainput);
dateinput = (EditText)dialog.findViewById(R.id.editDate);
final Calendar c = Calendar.getInstance();
dd = c.get(Calendar.DAY_OF_MONTH);
mm = c.get(Calendar.MONTH);
yy = c.get(Calendar.YEAR);
dateinput.setText(new StringBuilder().append(dd).append("/").append(mm+1).append("/").append(yy));
dialog.show();
weightinput = (EditText)dialog.findViewById(R.id.editWeight);
repsinput = (EditText)dialog.findViewById(R.id.editReps);
savebutton = (Button)dialog.findViewById(R.id.btnSave);
cancelbutton = (Button)dialog.findViewById(R.id.btnCancel);
savebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RelativeLayout views = (RelativeLayout)findViewById(R.id.xxx);
texty = new TextView(AnalysisPage.this);
texty.setTextSize(28);
texty.setText("Kool");
views.addView(texty);
}
});
cancelbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.cancel();
}
});
return true;
default:
return super.onOptionsItemSelected(item);
}
}
XML代码:
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:orientation="vertical"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent" 安卓:weightSum="1" 安卓:id="@+id/statslayout">
<RelativeLayout
安卓:layout_width="match_parent"
安卓:layout_height="match_parent" 安卓:id="@+id/xxx">
<TableRow
安卓:layout_width="fill_parent"
安卓:layout_height="100dp" 安卓:id="@+id/tableRow3" 安卓:layout_alignParentTop="true"
安卓:layout_alignParentStart="true" 安卓:layout_marginStart="72dp">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:text="Dumbbell Press Stats!"
安卓:id="@+id/textView"/>
</TableRow>
<TableRow
安卓:layout_width="fill_parent"
安卓:layout_height="100dp" 安卓:id="@+id/tableRow"
安卓:layout_alignParentTop="true" 安卓:layout_alignEnd="@+id/tableRow3"
安卓:layout_marginTop="49dp" 安卓:layout_alignBottom="@+id/tableRow3">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceMedium"
安卓:text="Date"
安卓:layout_weight="0.22"
安卓:id="@+id/btnDate"
/>
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceMedium"
安卓:text=" Weight"
安卓:layout_weight="0.22"
安卓:id="@+id/txtWeight"
/>
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceMedium"
安卓:text=" Reps"
安卓:layout_weight="0.22"
安卓:id="@+id/btnReps"
/>
</TableRow>
<TableRow
安卓:layout_width="wrap_content"
安卓:layout_height="200dp"
安卓:layout_below="@+id/tableRow" 安卓:layout_alignParentStart="true" 安卓:id="@+id/tableRow4">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceMedium"
安卓:text="20/08/15"
安卓:id="@+id/textView2" 安卓:layout_below="@+id/tableRow"
安卓:layout_alignStart="@+id/tableRow3"/>
</TableRow>
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceMedium"
安卓:text=" 8"
安卓:id="@+id/textView7"
安卓:layout_marginEnd="82dp" 安卓:layout_below="@+id/tableRow"
安卓:layout_alignEnd="@+id/tableRow3"/>
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceMedium"
安卓:text="50kg"
安卓:id="@+id/textView6"
安卓:layout_marginStart="43dp"
安卓:layout_below="@+id/tableRow3" 安卓:layout_toEndOf="@+id/tableRow4"/>
</RelativeLayout>
我的最终(梦想)目标是通过使用Javai生成完整的、格式完美的行,其中包含日期、权重和重复次数,并将其放在相关列中。和多行,或者使用Java完全格式化
图像(这是XML格式,不是Java格式)
# 1 楼答案
使用ListView而不是以编程方式添加文本视图。这将彻底解决你的问题。您将使用一个自定义适配器setAdapter(),ListView将其设置为它的适配器(该适配器提供列表及其数据),并在每次单击添加新项的按钮时添加新行元素。这意味着替换TableRow位,并在RelativeLayout中使用ListView。此外,您还可以向ListView添加一个自定义头视图,以替换您已经使用的头(来源:addHeaderView())
查看您的行,它们似乎有字段:“日期”、“重量”和“重复次数”。这意味着创建一个具有这三个字段的自定义Java对象,并使该对象成为ListView适配器元素的对象类型。如果你真的需要,我可以提供初学者代码,但使用我提供的文档来教你如何使用ListView会对你有最好的帮助
教程:ListView tutorial
编辑:另一个好处:ListView手柄允许用户滚动更多行,并将更多行添加到此列表中。选择ListView的更多理由:)