Adding custom View to LinearLayout dynamicly does not work

坚强是说给别人听的谎言 提交于 2019-12-13 02:41:39

问题


I have a layout like this:

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

This is how I inflate:

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

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.


回答1:


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



来源:https://stackoverflow.com/questions/34561765/adding-custom-view-to-linearlayout-dynamicly-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!