android中滑動小點實現

來源:互聯網
上載者:User

<LinearLayout 
        android:id="@+id/viewGroup" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_alignParentBottom="true" 
        android:layout_marginBottom="30dp" 
        android:gravity="center_horizontal" 
        android:orientation="horizontal" > 
    </LinearLayout> 

// group是R.layou.main中的負責包裹小圓點的LinearLayout. 
        group = (ViewGroup)main.findViewById(R.id.viewGroup); 
 
        viewPager = (ViewPager)main.findViewById(R.id.guidePages); 
 
        for (int i = 0; i < pageViews.size(); i++) { 
            imageView = new ImageView(GuideViewDemoActivity.this); 
            imageView.setLayoutParams(new LayoutParams(20,20)); 
            imageView.setPadding(20, 0, 20, 0); 
            imageViews[i] = imageView; 
            if (i == 0) { 
                //預設選中第一張圖片
                imageViews[i].setBackgroundResource(R.drawable.page_indicator_focused); 

            } else { 
                imageViews[i].setBackgroundResource(R.drawable.page_indicator); 
            } 
            group.addView(imageViews[i]); 
        }

  @Override 
        public void onPageSelected(int arg0) { 
            for (int i = 0; i < imageViews.length; i++) { 
                imageViews[arg0] 
                        .setBackgroundResource(R.drawable.page_indicator_focused); 

                if (arg0 != i) { 
                    imageViews[i] 
                            .setBackgroundResource(R.drawable.page_indicator); 
                } 
            }
 
        } 

<LinearLayout android:layout_width="wrap_content"
            android:layout_height="35dip"
            android:layout_gravity="bottom|center_horizontal"
            android:gravity="center" >
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center">
                <View
                    android:id="@+id/dot01"
                    style="@style/dot_style"
                    android:background="@drawable/dot_focused"/>
                <View
                     android:id="@+id/dot02"
                    style="@style/dot_style"
                    android:layout_marginLeft="10dip"/>
                <View
                     android:id="@+id/dot03"
                    style="@style/dot_style"
                    android:layout_marginLeft="10dip"/>
                <View
                     android:id="@+id/dot04"
                     style="@style/dot_style"
                     android:layout_marginLeft="10dip"/>
        </LinearLayout>
           
        </LinearLayout>

dot_style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="dot_style">
        <item name="android:layout_width">5dip</item>
        <item name="android:layout_height">5dip</item>
        <item name="android:background">@drawable/dot_normal</item>
        <item name="android:layout_marginLeft">1.5dip</item>
        <item name="android:layout_marginRight">1.5dip</item>
    </style>
</resources>

<?xml version="1.0" encoding="utf-8"?>  dot_focused
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <solid android:color="#FFFFFF" />
    <corners android:radius="5dip" />
</shape>

<?xml version="1.0" encoding="utf-8"?>   dot_normal
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <solid android:color="#aaFFFFFF" />
    <corners android:radius="5dip" />
</shape>

private void initDots() {
        listDots = new ArrayList<View>();
        listDots.add(findViewById(R.id.dot01));
        listDots.add(findViewById(R.id.dot02));
        listDots.add(findViewById(R.id.dot03));
        listDots.add(findViewById(R.id.dot04));
    }

((View)listDots.get(position)).setBackgroundResource(R.drawable.dot_focused);
((View)listDots.get(oldPosition)).setBackgroundResource(R.drawable.dot_normal);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.