开发者

Want to add textviews to LinearLayout in an existing layout.xml

开发者 https://www.devze.com 2023-03-25 03:02 出处:网络
I can\'t quite get this to work, hoping to get some hints, it seems like from my research the code should work, any thoughts would be greatly appreciated...

I can't quite get this to work, hoping to get some hints, it seems like from my research the code should work, any thoughts would be greatly appreciated...

I have an existing layout.xml file that includes:

<RelativeLayout style="@style/bodyLayout">
    <ScrollView 
        android:id="@+id/scrollBody"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="false"
    >
        <LinearLayout
            android:id="@+id/scrollLinearLayout"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
        >           

        </LinearLayout>
    </ScrollView>
</RelativeLayout>

Then I have programatic 开发者_如何学运维code as follows:

    LinearLayout ll = new LinearLayout(this);
    ll = (LinearLayout)findViewById(R.id.scrollLinearLayout);

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

    TextView tv = new TextView(this);
    tv.setId(1);
    tv.setTextSize(R.dimen.text_size_medium);
    tv.setText("test");
    tv.setLayoutParams(lp);
    ll.addView(tv);

The new TextView doesn't appear when the activity is displayed, I know I a missing something obvious... the new TextView should appear in the LinearLayout section...


I have run your code works fine

LinearLayout ll = (LinearLayout)findViewById(R.id.subLinear);

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

    TextView tv = new TextView(this);
    tv.setId(1);
    tv.setTextSize(15);
    tv.setText("test adding");
    tv.setLayoutParams(lp);
    ll.addView(tv);

if you add new View(anything) from any button click event then add this line

ll.invalidate();

it will refresh it your component

0

精彩评论

暂无评论...
验证码 换一张
取 消