gpt4 book ai didi

Android 滑动屏幕带圆圈指示

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:23 26 4
gpt4 key购买 nike

我是 android 的新手,我需要一些关于如何创建带有圆形指示器的教程的基本解释,如下图所示 enter image description here

我使用过 ViewPager,但它会移动整个屏幕以及我的圆圈图像,这看起来不太好。我正在阅读有关 JakeWhatron 的 viewPageIndicator 的信息,但我在寻找问题的核心解决方案。

最佳答案

这是使用 Jake Whatron 的库获取带有文本的圆圈指示器的方法

将此添加到 build.gradle 文件中

compile 'com.viewpagerindicator:library:2.4.1@aar'

将您想要的图像作为主布局的背景。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/hello_layour"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_background_image" // add your background image here
tools:context=".HelloActivity">

</RelativeLayout>

在你想要指示符和文本的地方添加这个

<LinearLayout
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:orientation="vertical">

<android.support.v4.view.ViewPager // the actual text you want will be shows here
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<com.viewpagerindicator.CirclePageIndicator // this is the circular indicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip" />

</LinearLayout>

创建一个新类添加以下数据(这是 viewpager 的适配器):

package com.your.packagename;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class WelcomePagerAdapter extends PagerAdapter {

// Declare Variables
private Context context;
private String[] title;
private String[] description;
private LayoutInflater inflater;

public WelcomePagerAdapter(Context context, String[] title, String[] description) {
this.context = context;
this.title= title;
this.description= description;
}

@Override
public int getCount() {
return title.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, int position) {

// Declare Variables
TextView titleView;
TextView descriptionView;

inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// layout inflator
View itemView = inflater.inflate(R.layout.welcome_pager, container,
false);

// title text holder
titleView = (TextView) itemView.findViewById(R.id.welcome_title);
titleView.setText(title[position]);

// description text holder
descriptionView= (TextView) itemView.findViewById(R.id.welcome_description);
descriptionView.setText(description[position]);

// add viewpager_item.xml to ViewPager
((ViewPager) container).addView(itemView);

return itemView;

}


@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((RelativeLayout) object);

}
}

为 viewpager 项目的布局创建一个 xml 文件..

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
xmlns:tools="http://schemas.android.com/tools">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/welcome_title"
android:paddingTop="15dp"
android:textSize="25sp"
android:textColor="#fff"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="@+id/welcome_title"
android:id="@+id/welcome_description"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:padding="15sp"
android:textColor="#fff" />

</RelativeLayout>

现在最后只需将其添加到教程 Activity 的 onCreate() 部分:

    // pager titles
String[] titles = new String[]{"Random Title One", "Random Title Two",
"Random Title Three", "Random Title Four"};

// pager descriptions
String[] descriptions= new String[]{"random small description example", "random small description example",
"random small description example", "random small description example"};

// Locate the ViewPager in viewpager_main.xml
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);

// Pass results to ViewPagerAdapter Class
PagerAdapter adapter = new WelcomePagerAdapter(this, titles, descriptions);

// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);

// ViewPager Indicator
CirclePageIndicator mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
mIndicator.setViewPager(viewPager);

关于Android 滑动屏幕带圆圈指示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35459580/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com