如何向我已经创建的 LinerLayout 添加另一个布局时间?

发布于 2024-12-26 06:34:50 字数 3055 浏览 2 评论 0原文

这是我的 Prent 布局 (main.xml)

<!-- ============================================================ -->
            <!--  All Employees are added here (added and More then one) -->
            <!-- ============================================================ -->
            <LinearLayout 
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/myLayout"
                android:orientation="vertical"
                android:layout_gravity="center_vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#00000000">

                <!-- ++++++++ here extra layout is added programmatically -->


            </LinearLayout>

这是我想要作为子项的另一个布局文件,例如 child.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout         
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout2"         
    android:layout_width="fill_parent"         
    android:layout_height="wrap_content"         
    android:layout_alignParentLeft="true"         
    android:layout_alignParentTop="true"         
    android:gravity="center_vertical"         
    android:orientation="horizontal" >          

    <Button             
        android:id="@+id/btn_left"             
        android:layout_width="100dp"             
        android:layout_height="wrap_content" 
        android:singleLine="false"   
        android:maxLines="2"         
        android:text="hello"/>
    <TextView             
        android:id="@+id/list_title"             
        android:layout_width="fill_parent"             
        android:layout_height="wrap_content"             
        android:layout_weight="0.5"             
        android:gravity="center_horizontal"             
        android:text="Photos"             
        android:textAppearance="?android:attr/textAppearanceLarge" />          
    <Button             
        android:id="@+id/btn_right"             
        android:layout_width="100dp"             
        android:layout_height="wrap_content"             
        android:text="Save" />     
</LinearLayout>

现在在我的 Main 活动中,我正在调用 main.xml 并根据 for 循环条件我想添加child.xml 的布局,每次我想设置 child.xml 的 TextView 的不同值

那么它是如何可能的呢?

我已经这样做了:

private void doCalculationForMultipleEmployee() {
    singleEmployee.setVisibility(View.GONE);
    for (int i = 0; i<=tempEmployerList.size()-1; i++) {  
        View repeatedLayout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.test);     
        ((TextView)repeatedLayout.findViewById(R.id.list_title)).setText("Employee"+i);     
        // customize repeatedLayout with other data     
        myLinearLayout.addChild(repeatedLayout);
    }
}

但是这样做之后,我在 .inflate 和 .addChild 处遇到语法错误

那么我哪里出了问题?请帮助我通过一些代码通过 for 循环添加它。

Here is my Prent layout (main.xml)

<!-- ============================================================ -->
            <!--  All Employees are added here (added and More then one) -->
            <!-- ============================================================ -->
            <LinearLayout 
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/myLayout"
                android:orientation="vertical"
                android:layout_gravity="center_vertical"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#00000000">

                <!-- ++++++++ here extra layout is added programmatically -->


            </LinearLayout>

Here is my another layout file that I want as child, say child.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout         
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout2"         
    android:layout_width="fill_parent"         
    android:layout_height="wrap_content"         
    android:layout_alignParentLeft="true"         
    android:layout_alignParentTop="true"         
    android:gravity="center_vertical"         
    android:orientation="horizontal" >          

    <Button             
        android:id="@+id/btn_left"             
        android:layout_width="100dp"             
        android:layout_height="wrap_content" 
        android:singleLine="false"   
        android:maxLines="2"         
        android:text="hello"/>
    <TextView             
        android:id="@+id/list_title"             
        android:layout_width="fill_parent"             
        android:layout_height="wrap_content"             
        android:layout_weight="0.5"             
        android:gravity="center_horizontal"             
        android:text="Photos"             
        android:textAppearance="?android:attr/textAppearanceLarge" />          
    <Button             
        android:id="@+id/btn_right"             
        android:layout_width="100dp"             
        android:layout_height="wrap_content"             
        android:text="Save" />     
</LinearLayout>

Now in my Main activity I am calling the main.xml and based on the for loop condition I want to add the layout of the child.xml and every time I want to set the different value of the TextView of child.xml

So how it is Possible?

I have done like this:

private void doCalculationForMultipleEmployee() {
    singleEmployee.setVisibility(View.GONE);
    for (int i = 0; i<=tempEmployerList.size()-1; i++) {  
        View repeatedLayout = LayoutInflater.from(getApplicationContext()).inflate(R.layout.test);     
        ((TextView)repeatedLayout.findViewById(R.id.list_title)).setText("Employee"+i);     
        // customize repeatedLayout with other data     
        myLinearLayout.addChild(repeatedLayout);
    }
}

But after doing that I got syntax error at .inflate and at .addChild

So where have I gone wrong? Please help me by some code to add it by for loop.

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

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

发布评论

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

评论(3

千と千尋 2025-01-02 06:34:50

只需创建一个如下所示的循环:

    LinearLayout parent=findViewById(R.id.myLayout);
    for(int i=0;i<list.size();i++)
    {
       LinearLayout llItem=(LinearLayout)LayoutInflater.createFromSource(R.layout.child, null);
       TextView txt= (TextView)llItem.findViewById(R.id.title);
       txt.setText(list.get(i));
       parent.add(llItem);
    }

Just create a loop like below:

    LinearLayout parent=findViewById(R.id.myLayout);
    for(int i=0;i<list.size();i++)
    {
       LinearLayout llItem=(LinearLayout)LayoutInflater.createFromSource(R.layout.child, null);
       TextView txt= (TextView)llItem.findViewById(R.id.title);
       txt.setText(list.get(i));
       parent.add(llItem);
    }
送君千里 2025-01-02 06:34:50

膨胀视图,然后添加到父视图

LinearLayout parent = (LinearLayout)findViewById(R.id.myLayout);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Layout child = inflater.inflate(R.layout.myLayout2, null);
TextView title = (TextView)child.findViewById(R.id.list_title);
parent.addView(child);

根据需要重复多次或使用循环

Inflate the view then add to the parent

LinearLayout parent = (LinearLayout)findViewById(R.id.myLayout);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Layout child = inflater.inflate(R.layout.myLayout2, null);
TextView title = (TextView)child.findViewById(R.id.list_title);
parent.addView(child);

Repeat as many times as necessary or use a loop

不美如何 2025-01-02 06:34:50

如果我正确理解你的问题,你需要在布局中并排显示按钮、文本视图和按钮。
如果你想要这个,你只需使用列表视图并使用自定义适配器,这样你就可以并排显示按钮、文本和按钮

If i understood your question correctly you need to display button, textview and button side by side in your layout.
If you want this you just take listview and use custom adapter, with this you can display button, text and button side by side

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