Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android LinearLayout in ScrollView

I tryning to get a LinearLayout in a ScrollView like this:

The greenspace should be wrap_content and the red one should take the remaining space.

But this is my Result:

This is my Code:

foreach(Ele el : elements) {}
    LinearLayout layout = new LinearLayout(getActivity());
    LayoutParams layout_parm = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    layout.setOrientation(LinearLayout.HORIZONTAL);
    layout.setLayoutParams(layout_parm);

    TextView tv = new TextView(getActivity());
    tv.setLayoutParams( new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1) );
    tv.setBackgroundColor(getActivity().getResources().getColor((R.color.red)));
    tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    tv.setText("Name...");
    tv.setPadding(0, 0, 0, getDP(5));
    layout.addView(tv);

    TextView iView = new TextView(getActivity());
    iView.setText("OTO");
    iView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
    iView.setBackgroundColor(getActivity().getResources().getColor((R.color.green)));
    iView.setLayoutParams( new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0) );
    layout.addView(iView);

    frg_layout.addView(layout); 
}

I am so confused! Maybe you can help me to find out my fail... Thank you!

The Parent-ViewGroup:

<LinearLayout 
    android:id="@+id/frg_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:gravity="top"
    android:orientation="vertical" />
like image 438
Flo Avatar asked Apr 27 '26 01:04

Flo


1 Answers

you change the layout orientation

 LinearLayout layout = new LinearLayout(getActivity());
LayoutParams layout_parm = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(layout_parm);

uset this type in xml

<LinearLayout 
 android:id="@+id/frg_layout"
 android:layout_width="match_parent"
 android:layout_height="wrap_content">

<TextView
android:id="@+id/TextView1"
android:layout_width="250dp"
android:layout_height="60dp"/>

<TextView
android:id="@+id/textview2"
android:layout_width="250dp"
android:layout_height="60dp"
/>
</linearlayout>
like image 98
Jai Avatar answered Apr 28 '26 15:04

Jai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!