Frage

I have built a relative layout in an XML file that I want to programatically add to a scrollview.

Is there a way to add the premade layout to the scrollview without writing the relative layout in the program as opposed to in an XML file beforehand?

Here is the xml file I have and you can see in it the relative that I want to programtically add on to the 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>
War es hilfreich?

Lösung

Ok, this is a very simple example, and does not efficiently handle View recycling, data manipulation, etc.

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;
        }
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top