xml - 创建动态 View ,多个 View X 次,获取/设置每个组的值

标签 xml android layout

我有一个 XML 格式的基本 View 。

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

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

如何为每个组命名(位置)、设置值(seekbar 值)和检索值。

最佳答案

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

您可以做的是创建一个包含布局和标题的新 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>

和 Activity :

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>

关于xml - 创建动态 View ,多个 View X 次,获取/设置每个组的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2216811/

相关文章:

基于 jquery 的 xml 编辑器,使用 xml 模式

Android:为什么不让每个方法都同步?

xml - 使用R中的XPath按值过滤xml节点

java - 在 Java 中使用 XSLT 将 HTML 转换为 XML

Javascript XSLT 转换,将 XML 存储在变量中

android - ImageView 如何链接到网页?

android - 如何使用 Expo 框架测量 React Native 中的文本

c++ - 绘制图形的算法

java - 为什么我的 JTable 取代了我的其他 JComponent

php - IE 将显示为两行,但我希望它显示为一行