-2

layout_weight in LinearLayout is driving me nuts. I've read reams of similar question on StackOverflow and the web but none address my problem. I've copied multiple samples I've encountered but still no joy. My layout (below) is very simple

I've tried multiple values for button widths (fill_parent, wrap_content, 0dp, etc). Also tried explicitly setting weightSum=5.0 in LinearLayout -- all width the same (bad) result.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
> 
<!-- TRIED BUTTON WIDTHS OF fill_parent, wrap_content, 0dp - SAME RESULT! -->
<!-- TRIED weightSum=5.0 FOR LinearLayout - SAME RESUT! -->
<Button 
  android:text="North"
  android:id="@+id/up"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1.0"
/>
<Button
  android:text="South"
  android:id="@+id/down"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1.0"
/>
  <Button
  android:text="West"
  android:id="@+id/left"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1.0"
  />
<Button 
    android:text="East"
    android:id="@+id/right"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1.0"
/>
<Button 
  android:text="Exit"
  android:id="@+id/done"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  android:layout_weight="1.0"
/>
</LinearLayout>

Don't know if it's related but this problem appeared shortly after my phone got an OTA update from KitKat to Lollipop. Also, I use this layout in a custom Dialog not an Activity as shown below.

Dlg = new Dialog( Ctx );
Dlg.setContentView(R.layout.nudge );
Dlg.setTitle( "Direction" );
Dlg.setCancelable(false);

The first image below shows the problem: the buttons don't fill the layout and text wraps.

Strangely, the layout looks just fine in the Eclipse Graphical Layout (second image).

enter image description here

enter image description here

I've spent days on this problem without joy. Please help!

smac89
  • 39,374
  • 15
  • 132
  • 179
DontPanic
  • 2,164
  • 5
  • 29
  • 56
  • `fill_parent` is **deprecated**. `wrap_content` should be a **fixed value**, in this case. `weightSum` is **useless**. **0dp** is correct (or weights won't work). Try using a `smaller font size`. – Phantômaxx May 16 '15 at 18:01
  • I tried wrap_content with no effect. Many suggest that fill_parent is now called match_parent (both = -1). That didn't work either though. Thanks for suggestion. – DontPanic May 16 '15 at 18:34
  • `match_parent` is **correct**. And so is **0dp**. Now, simply use a **smaller font**, as I already suggested. – Phantômaxx May 16 '15 at 18:48
  • 1
    I've met the same problem. The text inside your buttons is actually doing this. Use smaller font as @DerGolem suggested. – Kasun Dissanayake May 17 '15 at 05:19
  • That's too crude a hack for me to retain my self-respect :-) – DontPanic May 18 '15 at 17:50

2 Answers2

0

I got it! The problem was (apparently) that the window manager was overriding the fill_parent in the root LinearLayout. To stop this, I had to do the following:

            // CREATE DIALOG
            Dlg = new Dialog( Ctx );
            Dlg.setContentView(R.layout.mydialog );
            ...
            // SET DLG TO FULL WIDTH
            Window window = Dlg.getWindow();
            WindowManager.LayoutParams wlp;
            wlp = window.getAttributes();
            wlp.width=WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes( wlp );

This did not make it 100% full width but close (I'm guessing 90%) which was fine with me.

DontPanic
  • 2,164
  • 5
  • 29
  • 56
0

You are almost successfully. Let try:

  1. Add android:weightSum="5" in linearLayout
  2. Add android:testSize="..." in each button. At this point, you should fill these testSize in order to get the best view.
chibao
  • 23
  • 6