简体   繁体   中英

Adding views programmatically with a xml layout

I want to programmatically add a view to ScrollView that is apart of a Fragment layout.

I have attempeted the following code:

public class MessageDetail extends Fragment {

    View layout;
    TextView messageText;
    Conversation conversation;
    List<Message> messages;
    LayoutInflater inflater;
    ViewGroup container;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        this.inflater = inflater;
        this.container = container;
        layout = inflater.inflate(R.layout.messenger_detail, container,
                false);
        messageText = (TextView) layout.findViewById(R.id.tvMessage);

        return layout;

    }

    public void loadConversation(Conversation conversation) {

        messageText.setText(conversation.getSubject());

        LinearLayout messagesView = (LinearLayout)layout.findViewById(R.id.llMessageDetail);

        LinearLayout messageBody = new LinearLayout(getActivity());
        View body = inflater.inflate(R.layout.messagebody, container);


        messagesView.addView(body);

    }

}

messagebody.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llMessageBody"
    android:layout_width="fill_parent"
    android:layout_margin="5dp"
    android:layout_height="200dp"
    android:background="#353535"
    android:orientation="vertical" >
    <TextView 
        android:id="@+id/tvMSBodyFrom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="Research Station:"
        android:textColor="#FFFFAA"
        />
<ImageView 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/tomatohigh"
    />
</LinearLayout>

message_detail.xml

<?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:background="#d3d6da"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:background="#FFAA00" >

        <TextView
            android:id="@+id/tvMessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="20dp"
            android:text="Select a Message:"
            android:textColor="#353535"
            android:textSize="30dp" />
    </LinearLayout>

    <ScrollView
        android:id="@+id/svMessageDetail"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#9900AA"
        android:minHeight="300dp" >

        <LinearLayout
            android:id="@+id/llMessageDetail"
            android:layout_width="fill_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content" >

        </LinearLayout>
    </ScrollView>

</LinearLayout>

The final implementaion would see me loop through a list of messages and add the a view with the message_body layout and change each part of the layout to the correct values of each message

Any ideas how to achieve this?

You can do like this:

YourView varView = new YourView(this);
myButton.setLayoutParams(new YourLayout.LayoutParams());

yourParentLayout.addView(varView);

Example: Add a button in parent linear layout

Button myButton = new Button(this);
myButton.setLayoutParams(new LinearLayout.LayoutParams(
                                     LinearLayout.LayoutParams.FILL_PARENT,
                                     LinearLayout.LayoutParams.FILL_PARENT));

myLayout.addView(myButton);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM