我正在使用kotlin在android中使用viewpager来滑动图像及其文本。尽管布局中有文本,并且也将文本添加到视图中,但我仍无法查看文本。
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp"
android:fitsSystemWindows="true"
android:orientation="vertical">
<ImageView
android:id="@+id/sliding_image"
android:layout_width="match_parent"
android:layout_height="270dp"
/>
<TextView
android:id="@+id/sliding_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#200"
android:layout_centerHorizontal="true"
android:layout_below="@+id/sliding_image"
android:gravity="center"
/>
</RelativeLayout>
适配器的代码段:
class SliderAdapter:PagerAdapter{
var context:Context
private var images:Array<Int>
private var texts:Array<Int>
lateinit var inflater: LayoutInflater
constructor(context: Context,images:Array<Int>,texts:Array<Int>):super(){
this.context=context
this.images=images
this.texts=texts
}
override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object` as RelativeLayout
override fun getCount(): Int = images.size
override fun instantiateItem(collection: ViewGroup, position: Int): Any {
var image:ImageView
var text:TextView
inflater=context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var view = inflater.inflate(R.layout.splashscreenslider, collection,false)
image = view.findViewById(R.id.sliding_image)
image.setImageResource(images[position])
text = view.findViewById(R.id.sliding_text)
text?.text= texts[position].toString()
println("${text?.text}")
collection.addView(view)
return view
}
override fun destroyItem(collection: ViewGroup, position: Int, `object`: Any) {
collection.removeView(`object` as RelativeLayout)
}
}
顺便说一句,我不使用片段。我只使用视图。
感谢您的帮助。
答案 0 :(得分:0)
此问题的答案是修改布局文件。这对我有用。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="300dp"
android:fitsSystemWindows="true"
android:orientation="vertical">
<ImageView
android:id="@+id/sliding_image"
android:layout_width="match_parent"
android:layout_height="240dp"
/>
<TextView
android:id="@+id/sliding_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:layout_marginTop="4dp"
android:textColor="#200"
android:layout_centerHorizontal="true"
android:layout_below="@+id/sliding_image"
android:gravity="center"
/>
</RelativeLayout>
适配器中的代码段如下所示。
class SliderAdapter:PagerAdapter{
var context:Context
private var images:Array<Int>
private var texts:Array<Int>
private lateinit var inflater: LayoutInflater
constructor(context: Context,images:Array<Int>,texts:Array<Int>):super(){
this.context=context
this.images=images
this.texts=texts
}
override fun isViewFromObject(view: View, `object`: Any): Boolean = view == `object` as RelativeLayout
override fun getCount(): Int = images.size
override fun instantiateItem(collection: ViewGroup, position: Int): Any {
var image:ImageView
var textview:TextView
inflater=context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
var view = inflater.inflate(R.layout.splashscreenslider, collection,false)
image = view.findViewById(R.id.sliding_image)
image.setImageResource(images[position])
textview = view.findViewById(R.id.sliding_text)
textview?.setText(texts[position])
println("${textview?.text}")
collection.addView(view)
return view
}
override fun destroyItem(collection: ViewGroup, position: Int, `object`: Any) {
collection.removeView(`object` as RelativeLayout)
}
}
谢谢Mike的回复。