带描述的图像滑块
Imageslider with description
我有一个功能齐全的图像滑块,带有适配器和指示点。但现在我想用 TextView 为每个图像添加描述。任何人都知道如何做到这一点?谢谢。
编辑:见下面的代码
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private String[] imageUrls = new String[]{
"http://image1.png",
"http://image2.png",
"http://image3.png",
"http://image4.png",
"http://image5.png"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = findViewById(R.id.view_pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabDots);
tabLayout.setupWithViewPager(viewPager);
}
}
activity_main.xml:
<?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"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center" />
</android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android:id="@+id/tabDots"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:tabBackground="@drawable/tab_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp" />
</RelativeLayout>
从 xml 文件中删除图像视图。只保留 viewpager 元素。
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
然后您需要创建具有您想要的布局的自定义适配器。您可以按照此 https://inducesmile.com/android/understanding-android-viewpager-and-custom-pageradapter/
找到创建自定义适配器的步骤
然后你需要更改 viewPager.setAdapter(adapter);
以设置自定义适配器的对象
我有一个功能齐全的图像滑块,带有适配器和指示点。但现在我想用 TextView 为每个图像添加描述。任何人都知道如何做到这一点?谢谢。
编辑:见下面的代码
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private String[] imageUrls = new String[]{
"http://image1.png",
"http://image2.png",
"http://image3.png",
"http://image4.png",
"http://image5.png"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = findViewById(R.id.view_pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabDots);
tabLayout.setupWithViewPager(viewPager);
}
}
activity_main.xml:
<?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"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center" />
</android.support.v4.view.ViewPager>
<android.support.design.widget.TabLayout
android:id="@+id/tabDots"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:tabBackground="@drawable/tab_selector"
app:tabGravity="center"
app:tabIndicatorHeight="0dp" />
</RelativeLayout>
从 xml 文件中删除图像视图。只保留 viewpager 元素。
<android.support.v4.view.ViewPager
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
然后您需要创建具有您想要的布局的自定义适配器。您可以按照此 https://inducesmile.com/android/understanding-android-viewpager-and-custom-pageradapter/
找到创建自定义适配器的步骤然后你需要更改 viewPager.setAdapter(adapter);
以设置自定义适配器的对象