I created a very simple activity where I create and set the views in
java code instead of xml. The width I pass to the outer LinearLayout
though has no effect at all (200px). The view is displayed across the
entire width of the screen, no matter what value I pass here.
(Note that this is just sample code; I know that in a real app you don't
use fixed values nor px as unit. I just want to point out the issue I
found for easier clarification).
But if I use the xml layout instead where the outer LinearLayout has a
value of 200px, it's applied properly and the view only takes 200px of
the screen.
Why does setting a fixed width in java code for the outer layout has no
effect, as opposed to the width set in xml?
In the activity code below, un/commenting either one of the lines:
// setContentView(llOuter); // <-- using views from xml
setContentView(com.example.R.layout.main); // <-- using views from
code above
should result in the same layout, not?
Activity:
=========
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// outer linear layout
LinearLayout llOuter = new LinearLayout(this);
llOuter.setLayoutParams(new LinearLayout.LayoutParams(
200, // *** this param has no effect ***
LinearLayout.LayoutParams.FILL_PARENT
));
llOuter.setBackgroundColor(Color.parseColor("#00ff00"));
// inner linear layout
LinearLayout llInner = new LinearLayout(this);
llInner.setLayoutParams(new LinearLayout.LayoutParams(
100, // *** this param is applied correctly ***
LinearLayout.LayoutParams.FILL_PARENT
));
llInner.setBackgroundColor(Color.parseColor("#0000ff"));
llOuter.addView(llInner);
// setContentView(llOuter); // <-- using views
from xml
setContentView(com.example.R.layout.main); // <-- using views
from code above
}
}
main.xml
=========
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llOuter"
android:tag="asdd"
android:layout_width="200px"
android:layout_height="fill_parent"
android:background="#00ff00"
>
<LinearLayout
android:id="@+id/llInner"
android:layout_width="100px"
android:layout_height="fill_parent"
android:background="#0000ff"
/>
</LinearLayout>
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en