想要将文本视图添加到现有layout.xml中的LinearLayout

发布于 2024-11-27 17:04:01 字数 1213 浏览 4 评论 0原文

我无法完全让它工作,希望得到一些提示,从我的研究看来,代码应该可以工作,任何想法将不胜感激...

我有一个现有的layout.xml 文件,其中包括:

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

然后我有编程代码如下:

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

显示活动时,新的 TextView 不会出现,我知道我缺少一些明显的东西......新的 TextView 应该出现在 LinearLayout 部分......

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天暗了我发光 2024-12-04 17:04:01

我已经运行您的代码,它可以正常工作

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

如果您从任何按钮单击事件添加新视图(任何内容),然后添加此行,

ll.invalidate();

,它将刷新您的组件

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文