Devo fare uso dell'inflater.
hills.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="@drawable/hills"
android:orientation="vertical" >
</LinearLayout>
Parto dal presupposto che i LayoutParams siano già definiti nel layout esterno...
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setBackground(getResources().getDrawable(R.drawable.sky));
LayoutParams llParams=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
setContentView(ll,llParams);
LayoutInflater inflater=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout child=(LinearLayout)inflater.inflate(R.layout.hills, null);
ll.addView(child);
}
E ottengo questo:
Evidentemente devo creare i LayoutParams programmaticamente...
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setBackground(getResources().getDrawable(R.drawable.sky));
LayoutParams llParams=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
setContentView(ll,llParams);
LayoutInflater inflater=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout child=(LinearLayout)inflater.inflate(R.layout.hills, null);
LayoutParams childParams = new LayoutParams(0,LayoutParams.MATCH_PARENT,1);
ll.addView(child,childParams);
}
E ottengo...Creo un altro layout figlio programmaticamente e lo affianco:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll=new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setBackground(getResources().getDrawable(R.drawable.sky));
LayoutParams llParams=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
setContentView(ll,llParams);
LayoutInflater inflater=(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout child=(LinearLayout)inflater.inflate(R.layout.hills, null);
LayoutParams childParams = new LayoutParams(0,LayoutParams.MATCH_PARENT,1);
ll.addView(child,childParams);
LinearLayout child1=new LinearLayout(this);
child1.setBackground(getResources().getDrawable(R.drawable.w));
ll.addView(child1,childParams);
}
...e okay:



Nessun commento:
Posta un commento