I want to add a text view with margins into a Relativelayout by clicking a button, but whenever I add the setMargins to the LayoutParams, it make the TextView disappeared. Here is my code
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/test11">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/test"
android:onClick="onClick"
android:text="asdf"/>
</RelativeLayout>
Here is my code:
public class tet extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
}
public void onClick(View v){
TextView textView = new TextView(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 30, 30, 30);
textView.setText("asdf");
textView.setLayoutParams(layoutParams);
((RelativeLayout)findViewById(R.id.test11)).addView(textView);
}
}