从java文件动态设置视图

时间:2014-03-21 19:31:47

标签: android performance

我试图通过使用特定数量的线性布局并设置源图像来使java文件以动态方式设置活动视图 那么如何使用java代码创建特定数量的图像视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ScrollView
    android:id="@+id/scroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent">


    <ImageView
    android:id="@+id/menuline"
    android:layout_width="match_parent"
    android:layout_height="85dp" >

</ImageView>
</LinearLayout>    



</ScrollView>

1 个答案:

答案 0 :(得分:0)

创建一个ImageView(假设this中有Activity):

ImageView imageView = new ImageView(this);
imageView.set(...) //configure your imageView according your needs
...
imageView.setLayoutParam(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,85); //set width and height

将imageView添加到linearLayout:

linearLayout.addView(imageView);