添加自定义视图的LinearLayout动态地不起作用 [英] Adding custom View to LinearLayout dynamicly does not work

查看:146
本文介绍了添加自定义视图的LinearLayout动态地不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/recipe_coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Scroll View Layout -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <!-- Main Layout-->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:focusableInTouchMode="true">

            <!-- Recipe Image -->
            <ImageView
                android:id="@+id/recipe_image"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:adjustViewBounds="true"
                android:background="@color/background_3"
                android:scaleType="centerCrop"
                android:src="@drawable/no_image_placeholder"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true" />

            <!-- Toolbar -->
            <include
                android:id="@+id/toolbar"
                layout="@layout/transparent_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true" />

            <!-- Recipe Title And Recipe Info -->
            <LinearLayout
                android:id="@+id/layout_title_info"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/background_8"
                android:layout_below="@id/recipe_image">

                <!-- Recipe Title -->
                <TextView
                    android:id="@+id/recipe_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textColor="@color/white_text"
                    android:fontFamily="sans-serif-medium"
                    android:textSize="22sp"
                    android:background="@color/background_8"
                    android:paddingTop="48dp"
                    android:paddingBottom="8dp"
                    android:paddingLeft="8dp"
                    android:paddingRight="8dp"
                    android:text="Szarlotka z nadzieniem kakaowym" />

                <!-- Recipe Info -->
                <TableLayout
                    android:id="@+id/table_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:shrinkColumns="*"
                    android:stretchColumns="*"
                    android:padding="8dp">

                    <!-- Row 1 With 2 Columns -->
                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <!-- Column 1 [Kitchen/Meal Type] -->
                        <LinearLayout
                            android:layout_height="wrap_content"
                            android:layout_width="0dp"
                            android:orientation="vertical"
                            android:layout_weight="1"
                            android:layout_column="0">

                            <!-- Image With Text -->
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center|left"
                                android:orientation="horizontal">

                                <!-- Image Icon -->
                                <ImageView
                                    android:layout_width="18dp"
                                    android:layout_height="18dp"
                                    android:src="@drawable/ic_script"
                                    android:layout_marginRight="2dp"/>

                                <!-- Kitchen/Meal Type Label -->
                                <TextView
                                    android:layout_height="wrap_content"
                                    android:layout_width="match_parent"
                                    android:textSize="17sp"
                                    android:textColor="@color/teal_text"
                                    android:text="@string/recipe_kitchen_meal_type"/>

                            </LinearLayout>

                            <!-- Kitchen/Meal Type -->
                            <TextView
                                android:id="@+id/recipe_category"
                                android:layout_height="wrap_content"
                                android:layout_width="match_parent"
                                android:textSize="16sp"
                                android:layout_marginLeft="20dp"
                                android:textColor="@color/white_text"
                                android:fontFamily="sans-serif-light"
                                android:text="-"/>

                        </LinearLayout>

                        <!-- Column 2 [Difficulty Level] -->
                        <LinearLayout
                            android:layout_height="wrap_content"
                            android:layout_width="0dp"
                            android:orientation="vertical"
                            android:layout_weight="1"
                            android:layout_marginLeft="8dp"
                            android:layout_column="0">

                            <!-- Image With Text -->
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center|left"
                                android:orientation="horizontal">

                                <!-- Image Icon -->
                                <ImageView
                                    android:layout_width="18dp"
                                    android:layout_height="18dp"
                                    android:src="@drawable/ic_signal"
                                    android:layout_marginRight="2dp"/>

                                <!-- Difficulty Level Label -->
                                <TextView
                                    android:layout_height="wrap_content"
                                    android:layout_width="match_parent"
                                    android:textSize="17sp"
                                    android:textColor="@color/teal_text"
                                    android:text="@string/recipe_difficulty_lvl"/>

                            </LinearLayout>

                            <!-- Difficulty Level -->
                            <TextView
                                android:id="@+id/recipe_difficulty_lvl"
                                android:layout_height="wrap_content"
                                android:layout_width="match_parent"
                                android:fontFamily="sans-serif-light"
                                android:textSize="16sp"
                                android:layout_marginLeft="20dp"
                                android:textColor="@color/white_text"
                                android:text="-"/>

                        </LinearLayout>

                    </TableRow>

                    <!-- Row 2 With 2 Columns -->
                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp">

                        <!-- Column 1 [Preparation Time] -->
                        <LinearLayout
                            android:layout_height="wrap_content"
                            android:layout_width="0dp"
                            android:orientation="vertical"
                            android:layout_weight="1"
                            android:layout_column="0">

                            <!-- Image With Text -->
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center|left"
                                android:orientation="horizontal">

                                <!-- Image Icon -->
                                <ImageView
                                    android:layout_width="18dp"
                                    android:layout_height="18dp"
                                    android:src="@drawable/ic_timer"
                                    android:layout_marginRight="2dp"/>

                                <!-- Preparation Time Label -->
                                <TextView
                                    android:layout_height="wrap_content"
                                    android:layout_width="match_parent"
                                    android:textSize="17sp"
                                    android:textColor="@color/teal_text"
                                    android:text="@string/recipe_preparation_time"/>

                            </LinearLayout>

                            <!-- Preparation Time -->
                            <TextView
                                android:id="@+id/recipe_preparation_time"
                                android:layout_height="wrap_content"
                                android:layout_width="match_parent"
                                android:textSize="16sp"
                                android:layout_marginLeft="20dp"
                                android:fontFamily="sans-serif-light"
                                android:textColor="@color/white_text"
                                android:text="-"/>

                        </LinearLayout>

                        <!-- Column 2 [People Count] -->
                        <LinearLayout
                            android:layout_height="wrap_content"
                            android:layout_width="0dp"
                            android:orientation="vertical"
                            android:layout_marginLeft="8dp"
                            android:layout_weight="1"
                            android:layout_column="1">

                            <!-- Image With Text -->
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center|left"
                                android:orientation="horizontal">

                                <!-- Image Icon -->
                                <ImageView
                                    android:layout_width="18dp"
                                    android:layout_height="18dp"
                                    android:src="@drawable/ic_human"
                                    android:layout_marginRight="2dp"/>

                                <!-- People Count Label -->
                                <TextView
                                    android:layout_height="wrap_content"
                                    android:layout_width="match_parent"
                                    android:textSize="17sp"
                                    android:textColor="@color/teal_text"
                                    android:text="@string/recipe_people_count"/>

                            </LinearLayout>

                            <!-- People Count -->
                            <TextView
                                android:id="@+id/recipe_people_count"
                                android:layout_height="wrap_content"
                                android:layout_width="match_parent"
                                android:textSize="16sp"
                                android:layout_marginLeft="20dp"
                                android:fontFamily="sans-serif-light"
                                android:textColor="@color/white_text"
                                android:text="-"/>

                        </LinearLayout>

                    </TableRow>

                    <!-- Row 3 With 2 Columns -->
                    <TableRow
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="8dp">

                        <!-- Column 1 [Add Date] -->
                        <LinearLayout
                            android:layout_height="wrap_content"
                            android:layout_width="0dp"
                            android:orientation="vertical"
                            android:layout_weight="1"
                            android:layout_column="0">

                            <!-- Image With Text -->
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center|left"
                                android:orientation="horizontal">

                                <!-- Image Icon -->
                                <ImageView
                                    android:layout_width="18dp"
                                    android:layout_height="18dp"
                                    android:src="@drawable/ic_calendar"
                                    android:layout_marginRight="2dp"/>

                                <!-- Add Date Label -->
                                <TextView
                                    android:layout_height="wrap_content"
                                    android:layout_width="match_parent"
                                    android:textSize="17sp"
                                    android:textColor="@color/teal_text"
                                    android:text="@string/recipe_add_date"/>

                            </LinearLayout>

                            <!-- Add Date -->
                            <TextView
                                android:id="@+id/recipe_add_date"
                                android:layout_height="wrap_content"
                                android:layout_width="match_parent"
                                android:textSize="16sp"
                                android:fontFamily="sans-serif-light"
                                android:textColor="@color/white_text"
                                android:layout_marginLeft="20dp"
                                android:layout_marginTop="2dp"
                                android:text="-"/>

                        </LinearLayout>

                        <!-- Column 2 [Author Name] -->
                        <LinearLayout
                            android:layout_height="wrap_content"
                            android:layout_width="0dp"
                            android:orientation="vertical"
                            android:layout_weight="1"
                            android:layout_marginLeft="8dp"
                            android:layout_column="0">

                            <!-- Image With Text -->
                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:gravity="center|left"
                                android:orientation="horizontal">

                                <!-- Image Icon -->
                                <ImageView
                                    android:layout_width="18dp"
                                    android:layout_height="18dp"
                                    android:src="@drawable/ic_telegram"
                                    android:layout_marginRight="2dp"/>

                                <!-- Author Name Label -->
                                <TextView
                                    android:layout_height="wrap_content"
                                    android:layout_width="match_parent"
                                    android:textSize="17sp"
                                    android:textColor="@color/teal_text"
                                    android:text="@string/recipe_proposition"/>

                            </LinearLayout>

                            <!-- Author Name -->
                            <TextView
                                android:id="@+id/recipe_author_name"
                                android:layout_height="wrap_content"
                                android:layout_width="match_parent"
                                android:textSize="17sp"
                                android:layout_marginLeft="20dp"
                                android:fontFamily="sans-serif-light"
                                android:textColor="@color/white_text"
                                android:text="-"/>

                        </LinearLayout>

                    </TableRow>

                </TableLayout>

            </LinearLayout>

            <!-- Floating Like Button With Counter -->
            <LinearLayout
                android:id="@+id/like_counter_layout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:gravity="center"
                android:padding="8dp"
                android:layout_marginBottom="-53dp"
                android:layout_alignBottom="@id/recipe_image"
                android:layout_alignParentEnd="true">

                <!-- Like Floating Button -->
                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/recipe_like"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:clickable="true"
                    android:src="@drawable/ic_action_like" />

                <!-- Like Counter -->
                <TextView
                    android:id="@+id/recipe_counter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="3dp"
                    android:textSize="12sp"
                    android:textColor="@color/white_text"
                    android:layout_alignBottom="@id/recipe_like"
                    android:text="0"/>

            </LinearLayout>

            <!-- Add To Favourites Floating Button -->
            <android.support.design.widget.FloatingActionButton
                android:id="@+id/recipe_add_to_favourites"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:layout_marginBottom="-28dp"
                android:src="@drawable/ic_action_add_to_favourites"
                android:layout_toLeftOf="@id/like_counter_layout"
                android:layout_alignBottom="@id/recipe_image"/>

            <!-- Recipe Description -->
            <LinearLayout
                android:id="@+id/layout_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@color/background_9"
                android:padding="8dp"
                android:layout_below="@id/layout_title_info">

                <!-- Recipe Description Label-->
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="4dp"
                    android:fontFamily="sans-serif-medium"
                    android:textColor="@color/white_text"
                    android:textSize="22sp"
                    android:text="@string/recipe_description" />

                <!-- Recipe Description -->
                <TextView
                    android:id="@+id/recipe_description"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="8dp"
                    android:textSize="17sp"
                    android:fontFamily="sans-serif-light"
                    android:textColor="@color/white_text"
                    android:text="-"/>

            </LinearLayout>

            <!-- Comments -->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:paddingTop="8dp"
                android:paddingBottom="8dp"
                android:background="@color/background_2"
                android:layout_below="@id/layout_description">

                <!-- Comments Label-->
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginBottom="8dp"
                    android:fontFamily="sans-serif-medium"
                    android:textColor="@color/primary_text"
                    android:textSize="22sp"
                    android:text="@string/recipe_comments" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_gravity="center"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp">

                    <!-- New Comment Input -->
                    <EditText
                        android:id="@+id/recipe_new_comment"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:singleLine="true"
                        android:layout_weight="1"
                        android:hint="@string/hint_new_comment" />

                    <!-- Comment Button -->
                    <ImageButton
                        android:id="@+id/recipe_submit_comment"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="4dp"
                        android:background="@color/background_2"
                        android:tint="@color/icon_1"
                        android:src="@drawable/ic_add_comment"/>

                </LinearLayout>

                <!-- Latest Comments -->
                <LinearLayout
                    android:id="@+id/comment_items"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical">
                </LinearLayout>

                <!-- More Comments Button -->
                <android.support.v7.widget.AppCompatButton
                    android:id="@+id/recipe_more_comments"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:stateListAnimator="@null"
                    android:theme="@style/AppTheme.ButtonStyle4"
                    android:padding="17dp"
                    android:text="@string/btn_more_comments"/>

            </LinearLayout>

        </RelativeLayout>

    </ScrollView>

</android.support.design.widget.CoordinatorLayout>

我要动态地添加意见,这一部分:

I want to add views dynamicly to this part:

<!-- Latest Comments -->
<LinearLayout
    android:id="@+id/comment_items"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
</LinearLayout>

这是怎么吹:

private void inflateComments() {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    LinearLayout container = (LinearLayout) findViewById(R.id.comment_items);
    for (int i = 0; i < commentItems.size(); i++) {
        View commentView = inflater.inflate(R.layout.comment_item, null);

        TextView title = (TextView) commentView.findViewById(R.id.comment_title);
        TextView text = (TextView) commentView.findViewById(R.id.comment_text);
        TextView date = (TextView) commentView.findViewById(R.id.comment_post_date);

        title.setText(commentItems.get(i).getAuthorName() + commentItems.get(i).getAuthorName());
        text.setText(commentItems.get(i).getComment());
        date.setText(commentItems.get(i).getPostDate());

        container.addView(commentView);
    }
}

不幸的是它没有任何膨胀意见纳入这个LinearLayout中。布局是空白一片。任何想法,为什么它不工作?自定义布局ID是匹配的。

Unfortunately it does not inflate any views into this LinearLayout. Layout is blank inside. Any ideas why it does not work? Custom layout ids are matching.

推荐答案

尝试设置你的容器的高度,以 WRAP_CONTENT insted的match_parent

Try setting height of yours container to wrap_content insted of "match_parent"

这篇关于添加自定义视图的LinearLayout动态地不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆