ScrollView中的Android LinearLayout

时间:2015-03-18 10:05:07

标签: android scrollview android-linearlayout

我试图在ScrollView中获取LinearLayout,如下所示:

绿色空间应该是wrap_content,红色空间应该占用剩余的空间。

但这是我的结果:

这是我的代码:

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); 
}

我很困惑!也许你可以帮助我找出我的失败...... 谢谢!

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" />

3 个答案:

答案 0 :(得分:1)

更改布局方向

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

在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>

答案 1 :(得分:0)

我认为你大部分都是正确的,只需要做一些小的调整:

 for each(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());

        //adjust the last number here to make the first block wider or larger as you want
        tv.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2));
        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)));

        //adjust your layout param to be a weight instead of wrap_content
        iView.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1));
        layout.addView(iView);

        frg_layout.addView(layout);
    }

看看是否有效!另一种方法是构建一个我认为通常更干净的xml布局,但是以编程方式它应该以大致相同的方式工作。

答案 2 :(得分:0)

使用带有xml的LayoutInflater将带来解决方案!谢谢!