Using LinearLayout.LayoutParams
package app.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Test extends Activity {
TextView myTextView;
private static boolean inflate = true;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (inflate)
inflateXMLLayout();
else
constructLayout();
}
private void inflateXMLLayout() {
setContentView(R.layout.main);
myTextView = (TextView) findViewById(R.id.myTextView);
}
private void constructLayout() {
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
LinearLayout.LayoutParams textViewLP = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
myTextView = new TextView(this);
myTextView.setText("Hello World, HelloWorld");
ll.addView(myTextView, textViewLP);
addContentView(ll, lp);
}
}
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, HelloWorld"
/>
</LinearLayout>
Related examples in the same category