Android 运行时生成布局
我有一个复杂的xml布局,它有列表视图。列表视图中的一行包含几个文本字段,这些字段的间距相等。我正在使用textview来存储文本,然后最终将所有项目添加到行中…它工作得非常好。 但现在我有一个例子,我不确定从一个Web服务中可以得到多少文本字段。因此,我需要在运行时动态创建textview,填充它们,然后插入到列表中。。 运行时是否有声明、添加和填充新textview字段的方法? 或者两个字段之间是否存在实现间距的方法 第一次通话的结果Android 运行时生成布局,android,android-layout,android-listview,android-linearlayout,Android,Android Layout,Android Listview,Android Linearlayout,我有一个复杂的xml布局,它有列表视图。列表视图中的一行包含几个文本字段,这些字段的间距相等。我正在使用textview来存储文本,然后最终将所有项目添加到行中…它工作得非常好。 但现在我有一个例子,我不确定从一个Web服务中可以得到多少文本字段。因此,我需要在运行时动态创建textview,填充它们,然后插入到列表中。。 运行时是否有声明、添加和填充新textview字段的方法? 或者两个字段之间是否存在实现间距的方法 第一次通话的结果 __________________________ |
__________________________
|_____|_____|_____|______|
第二次通话结果
________________________________
|_____|_____|_____|______|______|
我尝试实现下面提供的解决方案(Kenny),但由于某些原因,我无法将视图添加到列表中。。下面是我的代码
public class HeaderAdapter extends ArrayAdapter<Header> {
final Header[] listSymbols;
private TextView textView;
private LinearLayout row;
public HeaderAdapter(Context context, int symResourceID,
Header[] objects) {
super(context, symResourceID, objects);
listSymbols = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.header_view, parent, false);
Header headerRec = listSymbols[position];
for(int i = 0 ; i < listSymbols.length;i++){
textView = new TextView(getContext());
textView.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, //Width of the view
ViewGroup.LayoutParams.WRAP_CONTENT));//Height of the view
textView.setId(i);
row.add??
}
}
公共类HeaderAdapter扩展了ArrayAdapter{
最终标题[]列表符号;
私有文本视图文本视图;
私人线路布置区;
public HeaderAdapter(上下文,int-symResourceID,
标题[]对象){
超级(上下文、符号资源ID、对象);
列表符号=对象;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
LayoutInflater充气器=(LayoutInflater)getContext()
.getSystemService(上下文布局\充气机\服务);
视图行=充气机。充气(R.layout.header_视图,父视图,false);
收割台headerRec=列表符号[位置];
对于(int i=0;i
称之为
setContentView(R.layout.main);
headerList.add(new Header("Symbol","Quantity","Price","Price Yesterday","52 Week High","52 Week Low","Change","Days Gain","Days Gain %","Returns"));
Header[] tmpHeaderList = headerList.toArray(new Header[headerList.size()]);
ArrayAdapter<Header> headerAdapter = new HeaderAdapter(this,R.layout.twoway_header_view,tmpHeaderList);
headerListView.setAdapter(headerAdapter);
setContentView(R.layout.main);
标题列表。添加(新标题(“符号”、“数量”、“价格”、“昨日价格”、“52周高点”、“52周低点”、“更改”、“天数增益”、“天数增益%”、“收益”));
Header[]tmpHeaderList=headerList.toArray(新的Header[headerList.size());
ArrayAdapter headerAdapter=新的headerAdapter(此,右布局。双向标题视图,tmpHeaderList);
headerListView.setAdapter(headerAdapter);
xml布局文件..主文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<HorizontalScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:scrollbars="none"
android:id="@+id/headerHv">
<ListView android:id="@+id/header_listView1"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="@color/white" android:cacheColorHint="#00000000"
android:smoothScrollbar="true" android:scrollbars="none" />
</HorizontalScrollView>
</LinearLayout>
定义行模板的文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView android:id="@+id/headerList" android:layout_width="80dp"
android:layout_height="wrap_content" android:textColor="#000000"
android:typeface="sans" android:textStyle="normal" />
</LinearLayout>
您可以在代码中执行此操作。在循环中声明TextView,并使用RelativeLayout将它们相互定位。以下是我从列表中动态生成自定义按钮的方法,您可以对TextView执行相同的操作:
//Setup Buttons
layout = (LinearLayout)findViewById(R.id.layoutBars);
int count = lBars.size();
for(int i = 0; i< count;i++){
final Bar b = lBars.get(i);
BarButton button = new BarButton(DDTBars.this, R.drawable.barsmall , b.getName().toUpperCase());
button.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
button.setId(i);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Run activity passing name of the bar to retrieve data
Intent i = new Intent(DDTBars.this, DDTBar.class);
i.putExtra("name", b.getName());
startActivity(i);
}
});
layout.addView(button);
}
//设置按钮
布局=(线性布局)findViewById(R.id.layoutBars);
int count=lBars.size();
for(int i=0;i
所以你可以试试:
//Setup TextViews
layout = (LinearLayout)findViewById(R.id.mylayout);
int count = myTextList.size();
for(int i = 0; i< count;i++){
TextView txtView = new TextView(this);
txtView.setLayoutParams(new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, //Width of the view
ViewGroup.LayoutParams.WRAP_CONTENT));//Height of the view
txtView.setId(i);
layout.addView(txtView);
}
//设置文本视图
布局=(线性布局)findViewById(R.id.mylayout);
int count=myTextList.size();
for(int i=0;i
@Kenny…谢谢…但这里您使用的是BarButton,textview有类似的功能吗?BarButton是我制作的自定义按钮,所有按钮都扩展了View类,textview也扩展了View类。因此,您可以执行完全相同的操作,只需添加一个textview而不是按钮。我已经扩展了我的答案,向您展示,请将问题标记为已回答将其从未答复列表中删除。@Kenny..谢谢您的解决方案..但我有一个适配器,它可以帮助我填充行..我无法在行中添加视图..您能帮我吗..我已经在问题中添加了代码