我想在布局中添加自定义视图,但该布局包含在另一个布局中包含的另一个布局中。 我的问题是我的自定义视图没有被夸大到布局,因为Android无法找到自定义布局的ID。
这是我的自定义布局的代码,也是它的XML代码。
类别:
class PackShotView(ctx : Context, attrs : AttributeSet?, defStyle : Int) : RelativeLayout(ctx,attrs,defStyle) {
init {
inflate(context, R.layout.pack_shot_view,this)
(findViewById<NetworkImageView>(R.id.packShotImage) as NetworkImageView).setDefaultImageResId(R.drawable.pack_shot_empty)
(findViewById<NetworkImageView>(R.id.packShotImage) as NetworkImageView).setErrorImageResId(R.drawable.pack_shot_empty)
if(isInEditMode){
(findViewById<TextView>(R.id.data) as TextView).text = context.getString(R.string.data_default_text)
(findViewById<TextView>(R.id.newsTitle) as TextView).text = context.getString(R.string.news_title_default)
}
}
constructor(ctx: Context) : this(ctx, null, 0)
constructor(ctx: Context, attrs: AttributeSet?) : this(ctx,attrs,0)
fun setNewsInfo(news: News?,
imageLoader: ImageLoader,
urlBuilder: String?){
(findViewById<TextView>(R.id.data) as TextView).text = news?.date
(findViewById<TextView>(R.id.newsTitle) as TextView).text = news?.name
news?.image?.let {
Log.v(context.getString(R.string.app_name),"Displaying image from URL $urlBuilder")
(findViewById<NetworkImageView>(R.id.packShotImage) as NetworkImageView).setImageUrl(urlBuilder,imageLoader)
}
}
}
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:showIn="@layout/content_main"
tools:context=".Presentation.MainActivity">
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/packShotImage"
android:layout_width="wrap_content"
android:layout_height="200dp"
tools:background="@drawable/pack_shot_empty" />
<TextView
android:id="@+id/data"
android:layout_width="202dp"
android:layout_height="wrap_content"
android:layout_above="@+id/newsTitle"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"
android:ellipsize="end"
android:maxLines="2"
android:minLines="1"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textStyle="normal|bold"
tools:text="Data" />
<TextView
android:id="@+id/newsTitle"
android:layout_width="202dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/packShotImage"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"
android:ellipsize="end"
android:maxLines="2"
android:minLines="1"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
android:textStyle="normal|bold"
tools:text="Text" />
</RelativeLayout>
布局我希望拥有自定义视图:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="@drawable/layout_bg"
android:focusableInTouchMode="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="musicristo.pt.app.Presentation.MainActivity"
tools:showIn="@layout/app_bar_main">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/youtube"
android:id="@+id/youtube"
android:layout_marginTop="35dp"
android:layout_alignTop="@+id/facebook"
android:layout_alignLeft="@+id/facebook"
android:layout_alignStart="@+id/facebook"
android:layout_marginBottom="30dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
tools:ignore="ContentDescription,RtlHardcoded" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Segue-nos"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="25sp"
android:textStyle="bold"
android:layout_above="@+id/youtube"
android:layout_centerHorizontal="true"
android:layout_marginBottom="17dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/facebook"
android:id="@+id/facebook"
android:layout_marginBottom="64dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
tools:ignore="ContentDescription" />
<musicristo.pt.app.Presentation.Widget.PackShotView
android:id="@+id/packShot"
android:layout_width="280dp"
android:layout_alignParentStart="false"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_height="200dp">
</musicristo.pt.app.Presentation.Widget.PackShotView>
<android.support.v7.widget.SearchView
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/EditTextBackground"
android:background="@drawable/edittextbackground_edit_text_holo_light"
android:visibility="visible"
android:queryHint="Pesquise um cântico..."
android:textColorHint="@android:color/darker_gray">
</android.support.v7.widget.SearchView>
</RelativeLayout>
上述布局的父级:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
android:focusableInTouchMode="true"
tools:context="musicristo.pt.app.Presentation.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/toolbarColor"
app:contentScrim="?attr/colorPrimary"
app:layout_collapseMode="pin"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<include
android:id="@+id/main"
layout="@layout/content_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar" />
</RelativeLayout>
根布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/backgroundColor"
android:fitsSystemWindows="true"
android:focusableInTouchMode="true"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:openDrawer="start">
<include
android:id="@+id/bar_main"
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
</include>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>