Reputation: 8431
I'm struggling with getting a subclassed GLSurfaceView to be weighted correctly in a linear layout. Ultimately, I want a GLSurfaceView to take up about 66% and a RelativeLayout to take about 33%, so simply enough the weights should be 2 and 1 respectively.
It's horizontally oriented, the app is forced to be landscape. I'm testing initially with just the extended view and a TextView but it is not at all behaving as expected. Tests with 2 TextViews work fine, but this XML does not:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<com.maximusdev.games.ttr.GSGLSurfaceView
android:id="@+id/gsglview"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="2" />
<TextView android:id="@+id/testtext"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Test" />
</LinearLayout>
Instead of the GLSurfaceView taking up only 2/3rds of the screen, it takes up almost all of it, the word Test smashed into a tiny space. Weights of both 1, also does something very similar. Giving the TextView a weight of 0, actually behaves as it should... only enough room is given for the word Test to display and the rest is given to the GLSurfaceView. What is really irritating, is if I use a vertical orientation on the LinearLayout, with both weights at 1 (and the height and width layout params wrap_content and fill_parent values switched) it works correctly. What is even more irritating, is that this next layout actually works, though it's completely contrary to what I've learned about weighting so far (using fill parent along with the orientation direction and the weights are behaving in reverse). Really hoping someone can shed some light on this for me.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<com.maximusdev.games.ttr.GSGLSurfaceView
android:id="@+id/gsglview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
<TextView android:id="@+id/testtext"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:text="Test" />
</LinearLayout>
Upvotes: 1
Views: 1538
Reputation: 98541
Your second solution is not what you are looking for. The correct solution is to give each View a width of 0dip and the weight you want (the weight is used to distribute the remaining empty space after measuring each child.)
Upvotes: 3