3

我是 Android 开发的新手。
是否可以使用自定义属性更改包含布局内的视图?

我的意思是这样的:

这是my_layoutxml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listItem"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<ImageView
    android:id="@+id/userImage"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:src="..." />
    <!-- this src attribute can be overrided using my own attribute in the iclude tage -->

<TextView
    android:id="@+id/userName"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="..." />
    <!-- this text attribute can be overrided using my own attribute in the iclude tage -->
</LinearLayout>

这就是我想要包含my_layout到活动布局中的方式:

<include layout="@layout/my_layout" userName="@string/userName1" userImage="@drawable/userImage1"/>

userNameuserImage覆盖android:textandroid:src属性的值。

我已经阅读了有关数据绑定的信息,但我的意思是不使用任何 Java 类。我只需要在数据标签中定义一个变量,my_layout其中type引用的值@string/@drawable/获取文本或图片。

可能吗?

4

2 回答 2

2

作为您的描述,您要发布userName1userImage1my_layout. 你做不到。而且您使用自定义视图的方式是错误的。标签“包含”也不是自定义标签。

您可以按照此示例使用自定义属性:

<com.amscf.view.InviteItemView
    android:id="@+id/invite_item_instagram"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:background="@drawable/item_color"
    android:paddingBottom="10dp"
    android:paddingTop="10dp"
    app:appicon="@drawable/icon_app_instagram"
    app:btntext=""
    app:coindesc="Post a invitation"
    app:cointext="Share to Instagram"
    app:showweek="false"/>

在视图中,InviteItemView您可以使用以下代码获取属性:appIconbtnText

TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.invite_item_view);
int refId = typedArray.getResourceId(R.styleable.invite_item_view_appicon, R.mipmap.ic_launcher);
String btnText = typedArray.getString(R.styleable.invite_item_view_btntext);

和是你将得到的refIdbtnText

将以下代码添加到您的attrs.xml中,这是invite_item_view

<declare-styleable name="invite_item_view">
    <attr name="appicon" format="reference"/>
    <attr name="cointext" format="string"/>
    <attr name="coindesc" format="string"/>
    <attr name="btntext" format="string"/>
    <attr name="showweek" format="boolean"/>
</declare-styleable>
于 2018-01-16T07:18:04.690 回答
1

<inlcude />标签是为了帮助您重用不同页面中的布局。一个例子是定义一个自定义的 ToolBar 并在Activities、Fragment 中重用。

没有办法像你提到的那样添加这个自定义标签,但是你可以直接在后面的代码中声明,而不需要 DataBinding:

例如,有了这个toolbar.xml

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:textColor="@color/colorWhite" />
</android.support.v7.widget.Toolbar>

在 中activity_main.xml,您有:

<include layout="@layout/toolbar"/>

MainActivity.java你可以定义这个:

private TextView title = (TextView) findViewById(R.id.toolbar_title);
title.setText("Whatever you want");
于 2018-01-16T08:04:33.080 回答