android自定義View(2):實現百分比自適應佈局
android介面適配難是歷史原因,我們只能想辦法解決。github上面已有一些佈局自適應的解決方案,今天我分享的是自定義控制元件:RelativieLayout自適應百分比寬高。直接上菜。
一,實現的效果圖
眼見為實,截圖所示,寬高都是50%,實現了自適應
二,實現的原理
其實很簡單,就是自定義兩個屬性:寬和高的百分比,讓自定義的view繼承 RelativeLayout。取出這兩個屬性的值,測量父佈局的寬高,乘百分比就是實際的寬高,然後確定在父控制元件中的位置。
三,自定義樣式屬性
在values資料夾中新建attrs.xml,內容如下:
layout_widthPercent和layout_heightPercent都是浮點型,0-1之間的值,代表百分百。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="percentRelativeLayout">
<attr name="layout_widthPercent" format="float"></attr>
<attr name="layout_heightPercent" format="float"></attr>
</declare-styleable >
</resources>
四,自定義percentRelativeLayout
簡單粗暴,根據xml佈局中的子view的LayoutParams獲取實際寬高。
public class PercentRelativeLayout extends RelativeLayout{
public PercentRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TODO Auto-generated constructor stub
}
public PercentRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public PercentRelativeLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
// TODO Auto-generated method stub
return new LayoutParams(getContext(), attrs);
}
//測量自己
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//獲取自身的寬高
int widthHint = View.MeasureSpec.getSize(widthMeasureSpec);
int heightHint = View.MeasureSpec.getSize(heightMeasureSpec);
for(int i = 0;i<this.getChildCount();i++){
View child = this.getChildAt(i);
//獲取孩子view的佈局屬性
ViewGroup.LayoutParams params = child.getLayoutParams();
float widthPercent = 0;
float heightPercent = 0;
//含有自定義的屬性,則獲取百分百
if(params instanceof PercentRelativeLayout.LayoutParams){
widthPercent = ((PercentRelativeLayout.LayoutParams) params).getWidthPercent();
heightPercent = ((PercentRelativeLayout.LayoutParams) params).getHeightPercent();
}
if(widthPercent == 0|| heightPercent == 0){
continue;//百分百為0,跳出此次迴圈
}
//真實的寬高
params.width = (int) (widthPercent*widthHint);
params.height = (int) (heightPercent*heightHint);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
super.onLayout(changed, l, t, r, b);
}
public static class LayoutParams extends RelativeLayout.LayoutParams{
private float widthPercent;
private float heightPercent;
public float getWidthPercent() {
return widthPercent;
}
public void setWidthPercent(float widthPercent) {
this.widthPercent = widthPercent;
}
public float getHeightPercent() {
return heightPercent;
}
public void setHeightPercent(float heightPercent) {
this.heightPercent = heightPercent;
}
//建構函式裡面獲取自定義樣式屬性的值
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
TypedArray array = c.obtainStyledAttributes(attrs, R.styleable.percentRelativeLayout);
widthPercent = array.getFloat(R.styleable.percentRelativeLayout_layout_widthPercent, widthPercent);
heightPercent = array.getFloat(R.styleable.percentRelativeLayout_layout_heightPercent,heightPercent);
array.recycle();
}
public LayoutParams(int w, int h) {
super(w, h);
// TODO Auto-generated constructor stub
}
public LayoutParams(android.view.ViewGroup.LayoutParams source) {
super(source);
// TODO Auto-generated constructor stub
}
public LayoutParams(MarginLayoutParams source) {
super(source);
// TODO Auto-generated constructor stub
}
}
}
五,佈局中引用自定義View和屬性
上菜了。不用 系統控制元件,用咱們自己的。
<com.example.view.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.example.percent_relativelayout_dn"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="測試百分比佈局"
android:layout_centerInParent="true"
app:layout_widthPercent = "0.5"
app:layout_heightPercent = "0.5"
android:background="#00ff00"
android:id="@+id/textview"
/>
</com.example.view.PercentRelativeLayout>
六,實現與總結
直接在activity中執行測試,是不是很完美了。寬高半分比佈局OK了。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
五大布局都可以實現,今天只是分享了RelativieLayout的中控制元件的百分百自適應。之前在github上面看了一個很全面的百分百佈局方案,其實原理就這樣。
歡迎交流,杜乾,Dusan,Q 291902259。
相關推薦
android自定義View(2):實現百分比自適應佈局
android介面適配難是歷史原因,我們只能想辦法解決。github上面已有一些佈局自適應的解決方案,今天我分享的是自定義控制元件:RelativieLayout自適應百分比寬高。直接上菜。 一,實現的效果圖 眼見為實,截圖所示,寬高都是50%,實現了自
自定義堆(2):通過堆實現優先佇列
學習堆、優先佇列之間的關係。 普通佇列:先進先出;後進後出。 優先佇列:出隊順序和入隊順序無關,和優先順序相關。 入隊 出隊(拿出最大元素) 之前自定義的普通線性結
自定義樹(2):二分搜尋樹(Binary Search Tree)
二分搜尋樹也是一種二叉樹。 二分搜尋樹的遍歷: 層序遍歷圖解: 刪除任意元素圖解: 程式碼實現: packa
自定義佇列(2):迴圈佇列
通過學習自定義佇列,瞭解佇列的資料結構。 首先寫一個佇列的介面,描述其具有的基本功能。Queue.java 然後寫一個介面的實現類,這只是其中一種實現方式,為迴圈佇列。LoopQueue.java 最後寫一個測試類,測試自定義陣列佇列的效果。Test.java
淺談安卓自定義view(一):製作一個最最最簡單的自定義view
對於安卓程式設計師來說,自定義view簡直不要太重要,畢竟有很多功能,譬如圓形頭像這些,用單純的原生非常難以實現,而用自定義view,簡直分分鐘。 在這裡,我嘗試用最簡單方式跟初學者說一下如何自定義一個自己的view~ 首先,最簡單最簡單的自定義view,有
自定義堆(1):實現最大堆
通過學習自定義堆,瞭解堆的資料結構。 本篇以最大堆為例。 底層依賴了自定義陣列, 參考:自定義陣列 中的 Array.java 所以,其時間複雜度分析: add
自定義View(四) Graphics2D 實現動態效果
自定義動畫的動態包括兩個方面 讓動畫動起來 (這類動畫可以通過週期行重畫實現) 實現和使用者的互動 在繪圖的整個過程中,經常會使用到雙快取技術,這是一項挺重要的技術,,為什麼這麼說呢? 能提高繪圖的效率 實現繪圖的過程和結果分離 ⚠️ 理解和掌
Android從零開搞系列:自定義View(9)事件分發+事件攔截(滑動衝突)
我和一幫應屆生同學維護了一個公眾號:IT面試填坑小分隊。旨在幫助應屆生從學生過度到開發者,並且每週樹立學習目標,一同進步! 寫在前面 今天用了一天的時間去再一次梳理了一遍,事件分發和事件攔截。用了這麼長時間倒不是因為理解深刻,,而是順便看了3
Android從零開搞系列:自定義View(4)基本的自定義ViewPager指示器+開源專案分析(上)
基本的自定義ViewPager的指示器 當然關於ViewPager指示器,如果只需要簡潔大方,那麼我們最簡單的方案就是使用TabLayout+ViewPager。 當然咱們也有很多非常不錯的開源框架可以選擇。 本次的記錄的內容就是
Android自定義View(三)-Draw原理篇
Android自定義View通常需要經過measure、layout和draw過程。 如果你沒有了解過measure過程,可以先看看這篇文章。 如果你沒有了解過layout過程,可以先看看這篇文章。 一、draw的作用:繪製View檢視 二、draw過程:類似meas
Android 自定義View(一)
前言:可是有時候我們總感覺官方定義的一些基本元件不夠用,自定義元件就不可避免了。那麼如何才能做到像官方提供的那些元件一樣用xml來定義他的屬性呢? 先總結下自定義View的步驟: 1、自定義View的屬性; 2、在View的構造方法中獲得自定義的屬性。 一、在re
Android學習—簡單自定義View(一)
最近手上不忙所以回顧了一下自己今年來所接觸和學習的東西,突然覺得寫部落格真是一個很好的方式,希望自己 可以堅持下去。 自定義View的流程 建立自定義類繼承View,並重寫構造方法,構造方法總共有四種,我們暫時只需要繼承前兩種 public CircleVi
android自定義View(一)、正弦波水波紋
文章目錄 1、正弦曲線知識 2、靜態正弦曲線繪製 3、動態正弦曲線繪製 4、完整原始碼 1、正弦曲線知識 對這個初中知識遺忘了的可以先看看正弦曲線百度百科詞條方便加深理解。
Android自定義view(一),打造絢麗的驗證碼
前言:我相信信念的力量,信念可以支撐起一個人,一個名族,一個國家。正如“人沒有夢想和鹹魚有什麼區別”一樣,我有信念,有理想,所以我正在努力向著夢想前進~。 自定義view,如果是我,我首先要看到自定義view的效果圖,然後再想想怎麼實現這種效果或功能,所以先貼
Android進階之自定義View(1)實現可換行的TextView
今天來一起學習一下最簡單的自定義view,自己動手寫一個MyTextView,當然不會像系統的TextView那麼複雜,只是實現一下TextView的簡單功能,包括分行顯示及自定義屬性的處理,主要目的是介紹自定義view的實現的基本思路和需要掌握的一些基礎知
android 自定義View(完美)
原文連結:http://www.jianshu.com/p/d507e3514b65 一、自定義View,你真的掌握了嗎? 什麼?你說你掌握了自定義View?來來來,回答老衲如下問題: Google提出View這個概念的目的是什麼?View這個概念與Activtiy、F
Android 自定義View(基礎)
一:引言 Android的開發中,在移動裝置上展示的所有內容,都是靠一個一個具體的檢視控制元件,按照一定的排列規則展示出來的,這些一個個控制元件,都是系統提供給我們的。但是我們看到,app商店上有些比較炫酷的頁面展示,我們會發現,系統根本沒有提供那些控制元件,
Android 自定義View(二),點,線的繪製
public class PointLine extends View { Paint mLinePaint; Paint mPointPaint; float width; float height; float pointAddress
自定義屬性(2)
aps 按鈕 charset script 定義 type i++ element color 定義三個按鈕 點擊按鈕,出現A、B、C、D字母,點擊一下,出現一個 當按鈕字母為 D 的時候,繼續點擊又會重新回到 A 1 <!DOCTYPE HTML>
自定義View(1)
點操作:moveTo和lineTo和rLinneTo的理解 1、lineTo 用於進行直線繪製。起點預設為座標原點(左上),如果有path的存在,則是繪製的最後點為基準,座標點對應的(0,0)到lineTo(x,y)的偏移量 比如 /** * 線操作 * lineTo的偏移量相對於原