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

huangapple go评论59阅读模式
英文:

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

问题

如何在LinearLayout内添加CardView,并访问CardView内部的TextView以更新值?在犹豫了很长时间后,我仍然没有解决方案。谢谢。

这是fragment_sub_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<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文件的内容:

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

以下是expanded_card.xml文件的内容:

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

<details>
<summary>英文:</summary>

How to add cardview inside Linearlayout, and access the textview inside the cardview to update the value? I still didn&#39;t have the solution after wondering around for a long time. Thanks

[Here&#39;s what I want][1]


  [1]: https://i.stack.imgur.com/fRlE8.png

This is the fragment_sub_main.xml
```&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;.SubMainFragment&quot;
    android:animateLayoutChanges=&quot;true&quot;
    android:id=&quot;@+id/relative_sub&quot;&gt;

    &lt;com.google.android.material.appbar.AppBarLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;&gt;
        &lt;androidx.appcompat.widget.Toolbar
            android:id=&quot;@+id/top_toolbar&quot;
            style=&quot;@style/Widget.AppTheme.AppBar&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;?attr/actionBarSize&quot;&gt;

        &lt;/androidx.appcompat.widget.Toolbar&gt;
    &lt;/com.google.android.material.appbar.AppBarLayout&gt;

    &lt;LinearLayout
        android:id=&quot;@+id/lin_1&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        tools:ignore=&quot;NotSibling&quot;&gt;

        &lt;include
            android:id=&quot;@+id/syarat&quot;
            layout=&quot;@layout/sub_service_card&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginStart=&quot;16dp&quot;
            android:layout_marginTop=&quot;64dp&quot;
            android:layout_marginEnd=&quot;16dp&quot; /&gt;
    &lt;/LinearLayout&gt;

    &lt;LinearLayout
        android:id=&quot;@+id/lin_2&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@id/lin_1&quot;&gt;

        &lt;include layout=&quot;@layout/sub_service_card&quot;
            android:id=&quot;@+id/langkah&quot;
            android:layout_marginTop=&quot;8dp&quot;
            android:layout_marginStart=&quot;16dp&quot;
            android:layout_marginEnd=&quot;16dp&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
    &lt;/LinearLayout&gt;

    &lt;LinearLayout
        android:id=&quot;@+id/lin_3&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_below=&quot;@id/lin_2&quot;&gt;

        &lt;include layout=&quot;@layout/sub_service_card&quot;
            android:id=&quot;@+id/lampiran&quot;
            android:layout_marginTop=&quot;8dp&quot;
            android:layout_marginStart=&quot;16dp&quot;
            android:layout_marginEnd=&quot;16dp&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot; /&gt;
    &lt;/LinearLayout&gt;

&lt;/RelativeLayout&gt;

Here's the sub_service_card

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;com.google.android.material.card.MaterialCardView xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
app:strokeWidth=&quot;0dp&quot;
app:strokeColor=&quot;@android:color/darker_gray&quot;
app:cardElevation=&quot;1dp&quot;
app:cardCornerRadius=&quot;16dp&quot;
app:cardPreventCornerOverlap=&quot;false&quot;&gt;
&lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:orientation=&quot;vertical&quot;
android:padding=&quot;16dp&quot;&gt;
&lt;com.google.android.material.textview.MaterialTextView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;Syarat Pengajuan UP&quot;
android:textAppearance=&quot;?attr/textAppearanceHeadline6&quot; /&gt;
&lt;/LinearLayout&gt;
&lt;/com.google.android.material.card.MaterialCardView&gt;

This is the expanded_card

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;com.google.android.material.card.MaterialCardView
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;match_parent&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
android:layout_height=&quot;wrap_content&quot;
app:strokeWidth=&quot;4dp&quot;
app:strokeColor=&quot;#C6C6C6&quot;
android:layout_marginTop=&quot;8dp&quot;
android:layout_marginStart=&quot;16dp&quot;
android:layout_marginEnd=&quot;16dp&quot;
app:cardElevation=&quot;0dp&quot;
android:animateLayoutChanges=&quot;true&quot;
app:cardBackgroundColor=&quot;@color/colorPrimaryDark&quot;&gt;
&lt;com.google.android.material.textview.MaterialTextView
android:id=&quot;@+id/text_in_card&quot;
android:padding=&quot;16dp&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:text=&quot;@string/pengUP&quot;
android:textAppearance=&quot;?attr/textAppearanceBody2&quot;/&gt;
&lt;/com.google.android.material.card.MaterialCardView&gt;

This is the oncreateview method in SubMainFragment

@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, &quot;Clicked&quot;);
textExpandView.setText(MainActivity.Langkah);
lin1.addView(view1);
//view1.addView(textExpandView);
}
});```
</details>
# 答案1
**得分**: 0
根据我的理解,您想要进行视图动画的代码翻译如下:
在您的注释中提到,您需要处理可见性。我很高兴看到它能够正常工作。
根据您的需求,您可以尝试以下方法来实现平滑效果:
**在布局中:**
```xml
android:animateLayoutChanges="true"

当可见性变为不可见时,进行淡出动画:

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

当可见性变为可见时,进行淡入动画:

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

请注意,上述代码是针对Android视图动画的示例。如果您需要更多关于Android开发方面的帮助,请随时提问。

英文:

As i write in comment you have to deal with visiblity and. I'm happy to see that it works

as you say you want to make this smooth then try this:

In layout

android:animateLayoutChanges=&quot;true&quot;

Fade out animation while visibltity invisible

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

Fade in animation while visiblity visible

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

huangapple
  • 本文由 发表于 2020年5月5日 13:13:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61606201.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定