2016-02-17 85 views
3

我是新來的Android,我需要了解如何創建圓圈指示符教程一些基本的解釋如下 enter image description here的Android輕掃屏幕,圓圈指示

顯示在圖像我用ViewPager但它一起移動整個屏幕我的圈子形象不好看。我正在閱讀關於JakeWhatron的viewPageIndicator,但我爲我的問題核心解決方案後。

+0

所以你要移動什麼?只是文字?這是來自目前正在處理的應用程序的示例 - https://drive.google.com/file/d/0B8CNLWhHBdgsdEhfSVpvMjNUUGM/view?usp=docslist_api我用Jake Whatrons庫btw :)你想要類似的東西嗎? – edwinj

+1

是的,我只想移動文本。 BTW你的登錄屏幕看起來不錯! – Lenny

回答

3

這是如何使用傑克Whatron圖書館

內容添加到文件的build.gradle

compile 'com.viewpagerindicator:library:[email protected]' 

把你想要的圖片作爲主要背景獲得文本圓圈指示符佈局。

<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> 

現在終於只需添加這到的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); 
+1

嗨edwinj!感謝您的詳細信息。我不明白Android在這方面的問題,從你的建議中找出一切。我在哪裏準確地把最後一點放在onCreate部分?目前我有:'WelcomePagerAdapter類','welcomepageradapter_layout.xml'和'fragment_a.xml'我需要更多嗎?感謝您的幫助和支持 – Lenny

+1

@Lenny您可以將它放在片段的onCreateView部分的任何位置。 (它只是onCreate,如果你使用的是btw活動):) – edwinj

+0

謝謝!如果我想要在視頻中顯示移動圖像,我應該在哪裏放置它們? – Lenny

1

使用ViewPager和CirclePageAdapter。

<com.movie.bms.utils.customcomponents.CustomViewPager 
     android:id="@+id/view_pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="@color/white"/> 

    <com.movie.bms.utils.customcomponents.CirclePageIndicator 
     android:id="@+id/circle_page_indicator" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     /> 

然後在您的活動中,將其附加到您的viewPager。

circlePageIndicator.setViewPager(viewPager); 

檢查此答案以獲取更好的解釋。

Viewpager indicator with images selected from gallery