如何实现在android中实现scrollview的动态布局中添加更多视图

如何实现在android中实现scrollview的动态布局中添加更多视图,android,views,android-linearlayout,Android,Views,Android Linearlayout,我在这里实现了自定义线性布局添加更多视图在后台使用WebService检索数据库所有数据都是检索消息和消息相关的用户图像在自定义线性布局中逐个加载图像后也会首先加载消息 main.xml <?xml version="1.0" encoding="utf-8"?> <FrameLayout android:id="@+id/frameLayout1" android:layout_width="fill_parent" android:background="@android:

我在这里实现了自定义线性布局添加更多视图在后台使用WebService检索数据库所有数据都是检索消息和消息相关的用户图像在自定义线性布局中逐个加载图像后也会首先加载消息

main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/frameLayout1" android:layout_width="fill_parent" android:background="@android:color/white"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent">
    <ScrollView android:id="@+id/scrollView1" android:layout_height="wrap_content" android:layout_width="fill_parent">
        <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout2" android:layout_height="fill_parent">
            <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout3" android:layout_height="fill_parent">
                <ImageView android:id="@+id/person" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
                <TextView android:textColor="@android:color/black" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</FrameLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayout1" 
    android:layout_width="fill_parent" 
    android:background="@android:color/white" 
    android:layout_height="fill_parent">
    <ScrollView android:id="@+id/scrollView1" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent">

    </ScrollView>
</FrameLayout>

代码文件:

public class MYlist extends Activity 
{
    /** Called when the activity is first created. */
    List<MessageClass> messages;
    int i=0,n=5;
     ParseXMLString parse=new ParseXMLString();
     DataParsingComm dataparsing=new DataParsingComm();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        String xml="<spGetUserMessages><SearchLocation></SearchLocation><LoginUserID>125</LoginUserID></spGetUserMessages>";
        messages =parse.GetGetUserMessages(dataparsing.ILGetUserMessages(xml));


        addElements(messages,i,n);
    }

    protected void addElements(final List<MessageClass> messages2,int i,int n) 
    {
        int k=0;
     //((LinearLayout)findViewById(R.id.linearLayout1)).removeAllViews();

        for(int i1=i;i1<n;i1++)
        {
            LinearLayout ll1=((LinearLayout)findViewById(R.id.linearLayout2));
            ll1.setBackgroundResource(R.drawable.tabmessage);
            Log.v("11111111","333"+ i1+ n);
          if(k==i1)
          {
            for(int j1=i1;j1<n;j1++)
            {

            TextView msg=((TextView)findViewById(R.id.textView1));
            msg.setText(messages2.get(j1).getmessage());

            }
          }
            ImageView prsnImg=((ImageView)findViewById(R.id.person));
        try{
            String photoXml="<spGetUserPhoto><UserID>125</UserID></spGetUserPhoto>";

            String photoInfoFromDB=new DataParsingComm().ILGetImage(photoXml);

            if(photoInfoFromDB.equalsIgnoreCase("anyType{}")||photoInfoFromDB.equals(null) )
            {
                prsnImg.setImageResource(R.drawable.person);
            }
            else{

                byte[] imgArry= Base64.decode(photoInfoFromDB);

                Bitmap bit=BitmapFactory.decodeByteArray(imgArry,0,imgArry.length);

                prsnImg.setImageBitmap(bit);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        finally
            {
                System.gc();
                System.runFinalization();
            }

        }

    }
}
公共类MYlist扩展活动
{
/**在首次创建活动时调用*/
列出信息;
int i=0,n=5;
ParseXMLString parse=新的ParseXMLString();
DataParsingComm dataparsing=新建DataParsingComm();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
字符串xml=“125”;
messages=parse.getUserMessages(dataparsing.ILGetUserMessages(xml));
附录(信息,i,n);
}
受保护的无效加法器(最终列表消息2,整数i,整数n)
{
int k=0;
//((LinearLayout)findViewById(R.id.linearLayout1)).removeAllViews();

对于(int i1=i;i1,这是因为您总是选择相同的两个对象

有一种非常基本的方法可以获得我认为你可能想要的东西:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/frameLayout1" android:layout_width="fill_parent" android:background="@android:color/white"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent">
    <ScrollView android:id="@+id/scrollView1" android:layout_height="wrap_content" android:layout_width="fill_parent">
        <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout2" android:layout_height="fill_parent">
            <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout3" android:layout_height="fill_parent">
                <ImageView android:id="@+id/person" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
                <TextView android:textColor="@android:color/black" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</FrameLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayout1" 
    android:layout_width="fill_parent" 
    android:background="@android:color/white" 
    android:layout_height="fill_parent">
    <ScrollView android:id="@+id/scrollView1" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent">

    </ScrollView>
</FrameLayout>

.爪哇

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ScrollView ScrollView=(ScrollView)findViewById(R.id.scrollView1);
LinearLayout topLinearLayout=新的LinearLayout(本);
topLinearLayout.setOrientation(LinearLayout.VERTICAL);
对于(int i=0;i<15;i++){
LinearLayout LinearLayout=新的LinearLayout(本);
linearLayout.setOrientation(linearLayout.HORIZONTAL);
ImageView ImageView=新的ImageView(此);
TextView TextView=新的TextView(此);
setImageResource(R.drawable.image);
setText(“文本视图”#“+i);
linearLayout.addView(图像视图);
linearLayout.addView(文本视图);
topLinearLayout.addView(linearLayout);
}
scrollView.addView(topLinearLayout);
}

您还可以在xml文件中定义
topLinearLayout
对象。这取决于您。

这是因为您总是选择相同的两个对象

有一种非常基本的方法可以获得我认为你可能想要的东西:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/frameLayout1" android:layout_width="fill_parent" android:background="@android:color/white"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent">
    <ScrollView android:id="@+id/scrollView1" android:layout_height="wrap_content" android:layout_width="fill_parent">
        <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout2" android:layout_height="fill_parent">
            <LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout3" android:layout_height="fill_parent">
                <ImageView android:id="@+id/person" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
                <TextView android:textColor="@android:color/black" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
            </LinearLayout>
        </LinearLayout>
    </ScrollView>
</FrameLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/frameLayout1" 
    android:layout_width="fill_parent" 
    android:background="@android:color/white" 
    android:layout_height="fill_parent">
    <ScrollView android:id="@+id/scrollView1" 
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent">

    </ScrollView>
</FrameLayout>

.爪哇

public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ScrollView ScrollView=(ScrollView)findViewById(R.id.scrollView1);
LinearLayout topLinearLayout=新的LinearLayout(本);
topLinearLayout.setOrientation(LinearLayout.VERTICAL);
对于(int i=0;i<15;i++){
LinearLayout LinearLayout=新的LinearLayout(本);
linearLayout.setOrientation(linearLayout.HORIZONTAL);
ImageView ImageView=新的ImageView(此);
TextView TextView=新的TextView(此);
setImageResource(R.drawable.image);
setText(“文本视图”#“+i);
linearLayout.addView(图像视图);
linearLayout.addView(文本视图);
topLinearLayout.addView(linearLayout);
}
scrollView.addView(topLinearLayout);
}

您还可以在xml文件中定义
topLinearLayout
对象。这取决于您。

我用一些您可以发现有用的基本代码更新了我的答案。我用一些您可以发现有用的基本代码更新了我的答案。