无法在 viewpager kotlin 中查看文本
Unable to view text in the viewpager kotlin
我起诉 viewpager 使用 kotlin 在 android 中滑动图像及其文本。尽管布局中有文本并将文本添加到视图中,但我无法查看文本。
布局代码:
<?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>
Adapter的代码片段:
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)
}
}
顺便说一句,我没有使用片段。我只使用视图。
感谢任何帮助。
这个问题的答案是修改布局文件。这对我有用。
<?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)
}
}
感谢迈克的回复。
我起诉 viewpager 使用 kotlin 在 android 中滑动图像及其文本。尽管布局中有文本并将文本添加到视图中,但我无法查看文本。
布局代码:
<?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>
Adapter的代码片段:
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)
}
}
顺便说一句,我没有使用片段。我只使用视图。
感谢任何帮助。
这个问题的答案是修改布局文件。这对我有用。
<?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)
}
}
感谢迈克的回复。