繁体   English   中英

如何以编程方式在视图内添加卡片视图,但使用 xml 中声明的布局?

[英]How to add cardview programmatically inside view but use layout declared in xml?

如何在Linearlayout里面添加cardview,并访问cardview里面的textview来更新值? 琢磨了半天,还是没有解决办法。 谢谢

这就是我想要的

这是 fragment_sub_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SubMainFragment"
    android:animateLayoutChanges="true"
    android:id="@+id/relative_sub">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/top_toolbar"
            style="@style/Widget.AppTheme.AppBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize">

        </androidx.appcompat.widget.Toolbar>
    </com.google.android.material.appbar.AppBarLayout>

    <LinearLayout
        android:id="@+id/lin_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="NotSibling">

        <include
            android:id="@+id/syarat"
            layout="@layout/sub_service_card"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="64dp"
            android:layout_marginEnd="16dp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/lin_1">

        <include layout="@layout/sub_service_card"
            android:id="@+id/langkah"
            android:layout_marginTop="8dp"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/lin_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/lin_2">

        <include layout="@layout/sub_service_card"
            android:id="@+id/lampiran"
            android:layout_marginTop="8dp"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

</RelativeLayout>

这是 sub_service_card

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:strokeWidth="0dp"
    app:strokeColor="@android:color/darker_gray"
    app:cardElevation="1dp"
    app:cardCornerRadius="16dp"
    app:cardPreventCornerOverlap="false">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">

        <com.google.android.material.textview.MaterialTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Syarat Pengajuan UP"
            android:textAppearance="?attr/textAppearanceHeadline6" />
    </LinearLayout>


</com.google.android.material.card.MaterialCardView>

这是扩展卡

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    app:strokeWidth="4dp"
    app:strokeColor="#C6C6C6"
    android:layout_marginTop="8dp"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    app:cardElevation="0dp"
    android:animateLayoutChanges="true"
    app:cardBackgroundColor="@color/colorPrimaryDark">

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/text_in_card"
        android:padding="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/pengUP"
        android:textAppearance="?attr/textAppearanceBody2"/>

</com.google.android.material.card.MaterialCardView>

这是 SubMainFragment 中的 oncreateview 方法

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View view = inflater.inflate(R.layout.fragment_sub_main, container, false);

        final LinearLayout lin1 = view.findViewById(R.id.lin_1);
        final MaterialCardView syaratView = lin1.findViewById(R.id.syarat);

        final View view1 = getLayoutInflater().inflate(R.layout.expanded_card, lin1, false);
        final MaterialTextView textExpandView = (MaterialTextView) (view1.findViewById(R.id.text_in_card));



        syaratView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Clicked");
                textExpandView.setText(MainActivity.Langkah);
                lin1.addView(view1);
                //view1.addView(textExpandView);

            }
        });```

正如我在评论中所写,您必须处理可见性和。 我很高兴看到它有效

正如你所说,你想让这一切顺利,然后试试这个:

在布局中

android:animateLayoutChanges="true"

在可见性不可见时淡出animation

view.animate().alpha(0.0f);

在可见性可见时淡入 animation

view.animate().alpha(1.0f);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM