如何在android中动态添加具有多列的行?
您好,我已经开发了如下屏幕所示的用户界面。现在我想添加用户界面中最后一行的“最后一个加号”按钮单击事件 我知道如何动态添加如何在android中动态添加具有多列的行?,android,user-interface,Android,User Interface,您好,我已经开发了如下屏幕所示的用户界面。现在我想添加用户界面中最后一行的“最后一个加号”按钮单击事件 我知道如何动态添加按钮或编辑文本,但我不知道如何动态添加和删除具有多个列的行 编辑:-关于最后一行,最重要的一点是每列都是可编辑的。这意味着我需要每列的引用来获取它的内容。当我动态添加行时,加号按钮应该放在新行中。同样,如果我删除行,加号按钮应该移到上一行 我怎样才能做到这一点。任何指导方针或任何方法都将不胜感激 这是我的XML文件 <?xml version="1.0" encodi
按钮
或编辑文本
,但我不知道如何动态添加和删除具有多个列的行
编辑:-关于最后一行,最重要的一点是每列都是可编辑的。这意味着我需要每列的引用来获取它的内容。当我动态添加行时,加号按钮应该放在新行中。同样,如果我删除行,加号按钮应该移到上一行
我怎样才能做到这一点。任何指导方针或任何方法都将不胜感激
这是我的XML文件
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/navi_bar" />
<Button
android:id="@+id/title_bar_btnBack"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="3dp"
android:background="@drawable/back_button_image" />
<Button
android:id="@+id/title_bar_btnExport"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/title_bar_btnBack"
android:layout_marginRight="10dp"
android:background="@drawable/export_button_normal" />
<!-- Create PDF Part 2 -->
<ImageView
android:id="@+id/pdf_Upper_Image"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/title_bar_btnBack"
android:background="@drawable/grid_bg_part1" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/pdf_Upper_Image"
android:layout_alignParentLeft="true"
android:text="Kassenbuch"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#3EC7F9"
android:textSize="23dp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="@+id/textView1"
android:text="Name :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_alignTop="@+id/textView2"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/textView2"
android:text="Akshay"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<!-- Custom Pdf Part 2 -->
<ImageView
android:id="@+id/pdf_Middle_Image"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/pdf_Upper_Image"
android:background="@drawable/grid_bg_part2"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
/>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/pdf_Middle_Image"
android:layout_marginLeft="2dp"
android:text="Einnahmen"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/txtName"
android:layout_alignTop="@+id/txtName"
android:layout_marginLeft="82dp"
android:layout_toRightOf="@+id/txtName"
android:text="Month :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_alignLeft="@+id/textView4"
android:layout_marginBottom="2dp"
android:text="Mand Nr. :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="@+id/txtMand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView5"
android:layout_alignTop="@+id/textView5"
android:layout_toRightOf="@+id/textView5"
android:text="Mand"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="@+id/txtMonth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/txtName"
android:layout_alignTop="@+id/textView4"
android:layout_toRightOf="@+id/textView5"
android:text="05"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp"
/>
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/txtYear"
android:layout_alignTop="@+id/txtMonth"
android:layout_toLeftOf="@+id/title_bar_btnExport"
android:text="Year :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView5"
android:layout_alignBottom="@+id/textView5"
android:layout_alignLeft="@+id/textView8"
android:text="Blatt :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="@+id/txtYear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/txtMonth"
android:layout_alignLeft="@+id/title_bar_btnExport"
android:layout_alignTop="@+id/txtMonth"
android:text="2012"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/pdf_Middle_Image"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/textView3"
android:text="Ausgabne"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9" >
</TextView>
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/pdf_Middle_Image"
android:layout_marginLeft="15dp"
android:layout_toRightOf="@+id/textView6"
android:text="Bestand"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9" />
<TextView
android:id="@+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/pdf_Middle_Image"
android:layout_toLeftOf="@+id/txtMand"
android:text="Datum"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9" />
<TextView
android:id="@+id/textView13"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/pdf_Middle_Image"
android:layout_toLeftOf="@+id/textView5"
android:text="Beleg Konto"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="8dp" />
<TextView
android:id="@+id/textView14"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView13"
android:layout_alignTop="@+id/pdf_Middle_Image"
android:layout_marginRight="03dp"
android:layout_toLeftOf="@+id/textView13"
android:text="Gegen Konto"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="08dp" />
<TextView
android:id="@+id/textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView14"
android:text="Anfangsbestand/Ubertrag"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="9dp" />
<TextView
android:id="@+id/textView11"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView13"
android:layout_alignTop="@+id/pdf_Middle_Image"
android:layout_marginLeft="25dp"
android:layout_toRightOf="@+id/textView12"
android:text="USt satz."
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="09dp" />
<TextView
android:id="@+id/txtBlatt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView9"
android:layout_alignBottom="@+id/textView9"
android:layout_toRightOf="@+id/textView9"
android:text="Blatt"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView11"
android:layout_alignLeft="@+id/textView9"
android:text="Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9" />
<include android:id="@+id/firstRow"
layout="@layout/custom_pdf3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/pdf_Middle_Image"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"/>
</RelativeLayout>
谢谢创建一个代表完整行的布局,然后您可以使用:
public-View-inflate(int-resource,ViewGroup-root)
来自LayoutInflater
(android.View包中的一个类)以从指定的XML资源(例如新行)充气新视图层次结构
现在,您可以使用特定的标记将此视图(您的行)动态添加到主布局中(就像将按钮动态添加到布局中)。我认为您可以通过一个示例来更好地理解。让我们把事情简化一点。假设您的主要布局如下:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_layout" >
</TableLayout>
然后是java部分。假设您想将其中的10行添加到TableLayout中,并分别处理每个行按钮。您可以这样做(您必须有权访问上下文对象):
//从xml获取主表布局
TableLayout布局=(TableLayout)findViewById(R.id.main_布局);
//获取一个更平坦的对象。”这是当前的活动
LayoutFlater充气机=LayoutFlater.from(本机);
对于(int i=0;i
创建一个XML布局文件,它将在列表视图中表示一行
使用基本适配器实现自定义适配器
在getView()
方法中,膨胀Xml布局,引用内部项(使用outerLayout.findViewById()
,将侦听器设置在那里,最后返回视图
为您的侦听器编写实现
足够简单吗?:-)您需要处理每个视图,然后每个视图都可以是一个复杂的视图,您可以随心所欲地执行任何操作,这项任务应该不容易
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="@+id/textView1"
android:text="Name :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<com.sample.MyCustomView
android:id="@+id/myView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
然后,MyCustomView
可以是线性布局或其他任何布局。尝试在运行时创建整个布局。创建包含所有ur列的单行,并将最后一行的可见性设置为“消失”。按下加号按钮时更改可见性。
我认为创建一行类型为相对布局会很好。当你想删除一行时,你必须计算主布局中的相对布局,然后删除你想删除的索引。在上面的xml中
您在Textview中完成了所有操作,并对其进行了调整
您需要在xml文件中创建一个Tablelayout
,而不是使用它
OnButton单击事件您必须动态创建一个新的tablerow,然后使用tablerow.addview(componentsname)
添加组件,最后将tablerow添加到tablelayout
在尝试了许多不同的方法后,我解决了如下问题。我对xml进行了某些更改。我使用了LinearLayout
和ScrollView
我创建了一个包含完整行的xml文件。现在,我的任务是在LinearLayout
中添加该xml,我按照以下方式执行。我每次都在按钮单击事件中调用此方法
public void AddDynamicView() {
linearLayout = (LinearLayout) findViewById(R.id.show_pdfTableLayout);
layoutInflater = (LayoutInflater) getApplicationContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.custom_pdf3, null);
linearLayout.addView(view);
list.add(view); // Here I have created list of View so that I can get the id of specific view.
}
我用TableLayout
尝试过这个方法,但是我在占用整行时遇到了一些问题。这就是为什么我使用了LinearLayout
可能我不太理解
您可以使用ListView和Adapter完成问题中提到的所有操作。
您可以提供一个数组来保存字段,用数组为适配器提供数据,用适配器为listView提供数据。
如果需要新行,只需向数组中添加一个元素。
若要删除行,只需将其从数组中删除即可。
如果要对项目进行排序,只需对数组进行排序。
如果您想选择prev/next,只需使用listview选择方法
我想知道你检查了你的答案,这不是绝对正确的方法。这是真的,但如果你的目标用户界面很复杂,页面将需要大量的时间来呈现和滚动,而不是动态和优化的列表视图
如果你认为我的答案听起来不错,请让我知道用一些代码更新它。谢谢你的重播,我会尝试这个。但是我有一个问题,我如何才能获得那一行中每个EditText
的引用。@Akshay请告诉我它是否正确。+1作为你的答案。我从你的答案中得到了一些想法。谢谢你的兄弟。这意味着我应该使用listview来更新它这是。但我不想在我的UI中做任何更改。我不明白你到底想说什么。你能建议我一些链接或更多信息吗?我已经尝试过了。但问题是,表行在添加行时出现了一些问题,它没有占用整行。它可以通过调整组件的大小来占用整行
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_marginLeft="10dp"
android:layout_marginTop="2dp"
android:layout_toRightOf="@+id/textView1"
android:text="Name :"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#3EC7F9"
android:textSize="12dp" />
<com.sample.MyCustomView
android:id="@+id/myView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
public void AddDynamicView() {
linearLayout = (LinearLayout) findViewById(R.id.show_pdfTableLayout);
layoutInflater = (LayoutInflater) getApplicationContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.custom_pdf3, null);
linearLayout.addView(view);
list.add(view); // Here I have created list of View so that I can get the id of specific view.
}