網站首頁 編程語言 正文
上節我們實現了一個圖片可以無限滑動的ViewPager,這一節我們需要自定義一個ViewPager來實現我們想要展現的布局
首先我們需要建一個包,然后新建一個java類,名字隨便起
這個類我們需要隨便繼承自一個viewGroup就行,viewGroup就是可以存放子控件的view,我們的各種layout,比如LinearLayour或者RelativeLayout這種可以在里面放東西的view,而TextView或者ImageView這種只能放內容而不能放其他view的就是普通view
然后我們選中三個構造器
package com.example.viewpager.views; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.RelativeLayout; import androidx.annotation.NonNull; import com.example.viewpager.R; import java.util.AbstractSet; public class LooperPager extends RelativeLayout { public LooperPager(Context context) { super(context); } public LooperPager(Context context,@NonNull AbstractSet abstrs) { super(context, (AttributeSet) abstrs); } public LooperPager(Context context,@NonNull AbstractSet abstrs,int defStyleAttr) { super(context, (AttributeSet) abstrs,defStyleAttr); } }
?然后我們在新建一個layout文件把想要實現的布局寫進去
因為我們是為ViewPager實現一個無限輪播的輪播圖,首先當然是寫一個ViewPager,然后是一個上方的標題,我們寫一個textview,因為想要和悲情區分開來,我們給背景設定為紅色,標題設定為白色,然后把文字居中,最后因為我們想要圖片在滑動時下方有一排根據圖片數量顯示滑動時代表圖片的標志的樣式,我們設定一個在控件底部居中顯示的線性布局,然后再線性布局內設定三個白色大小為5dp前后間隔為5dp的view
<?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="120dp" android:background="@color/colorAccent"http://背景設為紅色 android:orientation="vertical"> <androidx.viewpager.widget.ViewPager android:layout_width="match_parent" android:layout_height="match_parent" /> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我是標題" android:background="#ffffff "http://背景設為白色 android:textAlignment="center"http://居中 /> <LinearLayout android:layout_centerHorizontal="true"http://設為居中 android:layout_alignParentBottom="true"http://設為底部 android:layout_width="wrap_content" android:layout_height="wrap_content" > <View android:layout_width="5dp" android:layout_height="5dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#ffffff "/> <View android:layout_width="5dp" android:layout_height="5dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#ffffff "/> <View android:layout_width="5dp" android:layout_height="5dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="#ffffff "/> </LinearLayout> </RelativeLayout>
實現效果就是這樣的
?接下來就是把我們寫好的自定義布局綁定我們的自定義的類,因為我們想要無論調那個構造方法最后像都讓他去調我們寫綁定的方法,所以我們要把其他方法里面的supper都改成this
package com.example.viewpager.views; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.widget.RelativeLayout; import androidx.annotation.NonNull; import com.example.viewpager.R; import java.util.AbstractSet; public class LooperPager extends RelativeLayout { public LooperPager(Context context) { this(context,null); } public LooperPager(Context context,@NonNull AbstractSet abstrs) { this(context, abstrs,0); } public LooperPager(Context context,@NonNull AbstractSet abstrs,int defStyleAttr) { super(context, (AttributeSet) abstrs,defStyleAttr); //自定義布局綁定當前類,this:當前類,ture:確定綁定 LayoutInflater.from(context).inflate(R.layout.looper_pager,this,true); } }
?下一步就是實驗我們的自定義控件有沒有成功啦,
重新創建一個啟動文件然后在再創建一個lauout文件
,這里我們右鍵剛才的looppager選擇
?然后在新建的Layout文件里面粘貼設定好寬和高
<?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"> <com.example.viewpager.views.LooperPager android:layout_width="match_parent" android:layout_height="120dp"/> </RelativeLayout>
最后在我們新建的activity里面綁定剛才寫好的layout文件
package com.example.viewpager; import android.os.Bundle; import android.os.PersistableBundle; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; public class supper_MainActivity extends AppCompatActivity { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.supper_activity_main); } }
效果就實現了?
但剛開始寫完之后程序打開就報錯,我從凌晨一點開始找錯誤,找到兩點半發現布局文件里面的View寫成小寫view了,當時的心情不是一般的酸爽.....................
原文鏈接:https://blog.csdn.net/qq_42890648/article/details/122767276
相關推薦
- 2022-10-08 React中useState的使用方法及注意事項_React
- 2022-10-16 Django完整增刪改查系統實例代碼_python
- 2024-01-30 在 Jenkins 中使用 SSH Servers 配置文件上傳路徑
- 2023-02-17 python連接讀寫操作redis的完整代碼實例_python
- 2022-05-20 C++實現公司人事管理系統_C 語言
- 2022-12-22 Flutter組件開發過程完整講解_Android
- 2022-07-30 注冊中心eureka的介紹及源碼探索
- 2023-01-07 一篇文章徹底弄懂Python中的if?__name__?==?__main___python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支