我在XML文件中构建了一个相对布局,我想以编程方式添加到滚动视图中。
有没有办法将预制布局添加到scrollview而不在程序中编写相对布局而不是事先在XML文件中编写?
这是我拥有的xml文件,您可以在其中看到我想以编程方式添加到scrollview的相对文件。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:background="@android:color/black"
tools:context=".Threads" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:background="#364ACF" >
</LinearLayout>
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="55dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="@android:color/black" >
********Here is the layout**********
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical"
android:background="@drawable/layout_border" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/imageView1"
android:text="Linsey Sexton"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/textView_RecentMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView_Name"
android:layout_below="@+id/textView_Name"
android:text="Hey baby, I love you..."
android:textColor="@android:color/white" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
答案 0 :(得分:0)
好的,这是一个非常简单的例子,并没有有效地处理View回收,数据操作等。
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:background="@android:color/black"
tools:context=".Threads" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="55dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:background="#364ACF" >
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="55dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="@android:color/black" >
</ListView>
</RelativeLayout>
list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="60dp"
android:layout_height="60dp"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="5dp"
android:layout_toRightOf="@+id/imageView1"
android:text="Linsey Sexton"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/textView_RecentMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView_Name"
android:layout_below="@+id/textView_Name"
android:text="Hey baby, I love you..."
android:textColor="@android:color/white" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView listView = (ListView) findViewById(R.id.listView);
final ArrayList<Entry> entries = new ArrayList<Entry>();
entries.add(new Entry("Linsey Sexton", "Hey baby, I love you..."));
entries.add(new Entry("Joe Schmoe", "<Insert standard response here>"));
entries.add(new Entry("Linsey Sexton", "Joe, what are you saying?!"));
entries.add(new Entry("Joe Schmoe", "We're done, baby!"));
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.list_item, R.id.textView_Name, entries)
{
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
ImageView image = (ImageView) view.findViewById(R.id.imageView1);
TextView text1 = (TextView) view.findViewById(R.id.textView_Name);
TextView text2 = (TextView) view.findViewById(R.id.textView_RecentMessage);
image.setImageResource(R.drawable.ic_launcher);
text1.setText(entries.get(position).name);
text2.setText(entries.get(position).message);
return view;
}
};
listView.setAdapter(adapter);
}
class Entry
{
String name;
String message;
public Entry(String name, String message)
{
this.name = name;
this.message = message;
}
}
}