加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – 创建动态视图,多次视图X次,获取/设置每个组的值

发布时间:2020-12-16 05:34:58 所属栏目:百科 来源:网络整理
导读:我有一个 XML的基本视图. http://img513.imageshack.us/img513/7171/contextnotionlevelprefs.jpg http://img513.imageshack.us/img513/7171/contextnotionlevelprefs.jpg RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_pa
我有一个 XML的基本视图.

http://img513.imageshack.us/img513/7171/contextnotionlevelprefs.jpg http://img513.imageshack.us/img513/7171/contextnotionlevelprefs.jpg

<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content"><TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Family" android:id="@+id/Family" android:textSize="16px" android:padding="5px" android:textStyle="bold" android:gravity="center_horizontal"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/Family" android:layout_alignRight="@+id/Family" android:text="@string/context_notion_level_off" android:id="@+id/ContextNotionLevel"></TextView>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/Detailed" android:layout_below="@+id/Family" android:layout_alignLeft="@+id/Family" android:textStyle="bold" android:id="@+id/Module" android:text="Location"></TextView>
<SeekBar android:layout_height="wrap_content" android:id="@+id/SeekBar01" android:layout_width="fill_parent" android:padding="5px" android:saveEnabled="true" android:layout_below="@+id/Module" android:max="2"></SeekBar>

</RelativeLayout>

我怎样才能重复X次以下的部分,看起来像这样:
http://img687.imageshack.us/img687/4674/contextnotionleveldraft.png http://img687.imageshack.us/img687/4674/contextnotionleveldraft.png

然后,我如何访问所有项目的搜索栏?
请注意,我希望这是动态的,例如:15次.

如何为每个组提供名称(位置),设置值(搜索栏值)和检索值.

从你的照片,你不想重复“家庭”标题

你可以做的是创建一个包含布局和标题的新xml(mymain.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:id="@+id/myMainLayout" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content">
<TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Family" android:id="@+id/Family" android:textSize="16px" android:padding="5px" android:textStyle="bold" android:gravity="center_horizontal"></TextView>
</LinearLayout>

然后在另一个xml(myviews.xml)中,放入你从OP中放入的xml(删除标题)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
    android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content">

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:textStyle="bold"
        android:id="@+id/Module" android:text="Location">
    </TextView>

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="context_notion_level_off"
        android:layout_alignRight="@+id/Module" android:id="@+id/ContextNotionLevel">
    </TextView>


    <SeekBar android:layout_height="wrap_content" android:id="@+id/SeekBar01"
        android:layout_width="fill_parent" android:padding="5px"
        android:saveEnabled="true" android:layout_below="@+id/Module"
        android:max="2">
    </SeekBar>

</RelativeLayout>

和活动:

super.onCreate(savedInstanceState);

setContentView(R.layout.mymain);
LinearLayout l = (LinearLayout) findViewById(R.id.myMainLayout);
LayoutInflater linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

for(int i = 0 ; i < 15; i++) {
  View myView = linflater.inflate(R.layout.myviews,null);
  SeekBar s = (SeekBar) myView.findViewById(R.id.SeekBar01);
  //Set an id to know which seekbar is in use
  s.setId(i);
  TextView t = (TextView) myView.findViewById(R.id.Module);

  //Change name dynamically
  t.setText(("My location " + i).toString());
  s.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

    @Override
    public void onProgressChanged(SeekBar seekBar,int progress,boolean fromUser) {
        Log.i("=========","My SeekBar  = " + seekBar.getId());
        Log.i("=========","My Progress = " + progress);

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

    }});
  l.addView(myView);
}

编辑:

要添加滚动条,请在mymain.xml中的LinearLayout周围添加ScrollView标记

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:orientation="vertical" android:scrollbars="vertical">
<LinearLayout android:orientation="vertical" android:id="@+id/myMainLayout" android:layout_width="fill_parent"  android:layout_height="wrap_content">
<TextView android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Family" android:id="@+id/Family" android:textSize="16px" android:padding="5px" android:textStyle="bold" android:gravity="center_horizontal"></TextView>
</LinearLayout>

</ScrollView>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读