简体   繁体   中英

Add a RelativeLayout dynamically to a LinearLayout in Android

I have been trying for a while to work out how to dynamically create a RelativeLayout with multiple views inside (eg TextView, ProgressBar) a LinearLayout to create a RelativeLayout beneath the previous one after every button click. Please can anyone look at my code and see if there is anything that I can do to solve this issue.

Here is the code:

activity_test_container.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/frag1ScrollView"         
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" >

<LinearLayout
android:id="@+id/testLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".TestContainerActivity" >

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/testContainerTextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/testContainerTextView1"
        android:layout_marginBottom="16dp"
        android:text="TextView2" />

    <TextView
        android:id="@+id/testContainerTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="TextView1" />

    <Button
        android:id="@+id/testContainerButton1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/testContainerTextView2"
        android:text="Button" />
</RelativeLayout>

</LinearLayout>

</ScrollView>

container.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/containerLayout"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:layout_marginBottom="16dp"
    android:background="@color/display_panels" >

<ProgressBar
    android:id="@+id/containerProgressBar1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/containerImageButton2"
    android:max="100"
    android:progress="40" />

<TextView
    android:id="@+id/containerTextView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/containerTextView6"
    android:layout_alignLeft="@+id/containerProgressBar1"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/containerTextView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/containerProgressBar1"
    android:layout_centerHorizontal="true"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceSmall" />

<ImageButton
    android:id="@+id/containerImageButton2"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/containerTextView6"
    android:background="@color/display_panels"
    android:contentDescription="Okay icon"
    android:src="@drawable/ic_green_ok" />

</RelativeLayout>

TestContainerActivity.java

public class TestContainerActivity extends Activity implements OnClickListener {

LinearLayout containerLayout;
Button testButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_container);

    testButton = (Button)findViewById(R.id.testContainerButton1);
    containerLayout = (LinearLayout)findViewById(R.id.testLayout);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.test_container, menu);
    return true;
}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v==testButton){

        createNewLayout();

    }
}

public void createNewLayout(){

        LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View addView = layoutInflater.inflate(R.layout.container, null);

        containerLayout.addView(addView);

}

}

I'm not entirely sure what you're problem is, but I suspect it's that the rows are not showing up at all because I don't see where you attach the listener to the Button. To handle a click event, an OnClickListener needs to be set on your View. Though this is commonly done with Buttons, OnClickListeners can be set on any view, so any size/shape widget can be made clickable. This is done with the setOnClickListener method of a View. There are multiple ways to do this, try modifying your onCreate like this:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_container);

    testButton = (Button)findViewById(R.id.testContainerButton1);
    containerLayout = (LinearLayout)findViewById(R.id.testLayout);

    testButton.setOnClickListener(this);
}

An alternative method to setting your listener would be to create the listener in onCreate rather than using the Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_container);

    testButton = (Button)findViewById(R.id.testContainerButton1);
    containerLayout = (LinearLayout)findViewById(R.id.testLayout);

    testButton.setOnClickListener(new View.OnClickListener() {  
        public void onClick(View v) {
            createNewLayout();
        }
    });
}

In this case, you wouldn't need to have your Activity implement OnClickListener. I usually only will do something like that if I have many buttons with similar functionality, where creating listeners for each will cause a performance hit. For more isolated cases like this, I prefer to set individual Listeners since the performance difference will be negligible, but that's just my personal preference.

Hope this helps! If your problem was actually based somewhere else, please modify your question and I'll try my best to assist! Also, keep in mind that you can use the Log class to post information about execution in your LogCat output. It really helps with debugging! I suspect that if you put some logging in your listener and createNewLayout() right now, you'd see that the logging never happens because those methods are never called.

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