2

我正在尝试创建一个名为WeekView的自定义 CardView 对象。WeekView扩展了 CardView。我在 XML 文件中有一个 CardView 布局。如何让WeekView从 XML 文件中“复制”布局?

简单地说,我希望能够做到这一点:

WeekView weekCard = new WeekView();
mainLayout.addView(weekCard);

在主活动中,其中mainLayout是一个空的线性布局。WeekCard 应该看起来像 XML 布局文件中的卡片。

这是 XML 文件的缩短版本:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:tipbook="http://schemas.android.com/apk/res-auto"
android:id="@+id/week_card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
tipbook:contentPadding="12dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:text="Week View"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/week_view_title"
        android:textSize="12sp"
        android:textAlignment="gravity"
        android:gravity="center"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:textColor="@android:color/darker_gray"
        android:layout_marginTop="4dp"
        android:layout_marginLeft="4dp" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tipbook:srcCompat="@drawable/ic_more_vert_black_18dp"
        android:id="@+id/week_settings_btn"
        android:background="@color/cardview_light_background"
        android:scaleX="1"
        android:scaleY="1"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:tint="@color/cardview_shadow_start_color"
        android:tintMode="src_in"
        android:layout_marginRight="4dp"
        android:layout_marginTop="4dp" />


    <LinearLayout
        android:id="@+id/week_days"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_below="@id/week_settings_btn"
        android:weightSum="7"
        android:layout_marginTop="12dp" >



    </LinearLayout>

    <TextView
        android:text="Last Week"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/week_days"
        android:id="@+id/visible_week"
        android:textSize="14sp"
        android:textAlignment="gravity"
        android:gravity="center"
        android:layout_marginTop="12dp" />

    <ImageButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/week_days"
        android:layout_marginTop="12dp"
        tipbook:srcCompat="@drawable/ic_expand_more_black_18dp"
        android:id="@+id/more_less_week"
        android:scaleX="1"
        android:scaleY="1"
        android:tint="@color/common_google_signin_btn_text_light_default"
        android:tintMode="src_in"
        android:background="@color/cardview_light_background"
        android:visibility="gone"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
4

2 回答 2

2

include在主视图中使用链接到该卡片视图的标签

<include
   android:id="@+id/some_id"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   layout="@layout/your_card_layout" />

包括要重用的布局。

访问它的内部视图

View includedLayout = findViewById(R.id.some_id);
Button buttonInsideTheIncludedLayout = (Button)includedLayout.findViewById(R.id.button);
于 2016-12-21T02:17:00.883 回答
0

也许这个答案为时已晚,但我是 Android 开发的新手,我认为你在寻找的是LayoutInflater.inflate()方法。

扩展 XML 布局并将其View作为CardView.

我的代码示例:

LayoutInflater inflater = LayoutInflater.from(getContext());
CardView newSessionGroupCardView = (CardView) inflater.inflate(R.layout.session_group_cardview, null);

LinearLayout linearParentLayout = getView().findViewById(R.id.session_group_layout);
linearParentLayout.addView(newSessionGroupCardView);

也许这对某人有帮助。进一步阅读Layout Inflater

于 2018-07-09T16:52:25.553 回答