2

i tried to get the android:layout_weight in my function. this is my xml code:

    <LinearLayout
        android:id="@+id/myID"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="14"
        android:clickable="true"
        android:orientation="horizontal">
    ......
    </LinearLayout>

And the Part of my function:

import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableLayout.LayoutParams;

....
LinearLayout layout = (LinearLayout) findViewById(R.id.myID);

But when i try to call layout.getLayoutParams() there is no weight variable, only width, heigth, ... Is this beacause of the imported packages or why does getLayoutParams() not have weight?

this is the same issue as here, but he also couldn't solve it: How do I get the `layout_weight` of a TextView?

Community
  • 1
  • 1
vtni
  • 950
  • 3
  • 14
  • 43

1 Answers1

2

The actual type of the layout parameters that are used in a view depend on the type of container it sits in. Not all layout parameter types have a weight field. If your layout is part of another LinearLayout (or one of its derived classes, such as TableLayout or TableRow), then you simply need to cast the value returned from layout.getLayoutParams() to the correct type. For instance:

float weight = ((LinearLayout.LayoutParams) layout.getLayoutParams()).weight;

You don't say exactly what kind of layout class your layout view lives in, so I can't be any more specific than this.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • works for me! Thank you very much! @TedHopp Please, do you think you could help me with this question http://goo.gl/wO9G2N – eddy Sep 15 '14 at 13:55