本文實例為大家分享了android繪制多個黑豎線條展示的具體代碼,供大家參考,具體內容如下
1.寫一個LinearLayout的布局,將寬度寫成5dp將高度寫成match_parent.
2.在寫一個類繼承LinearLayout,用LayoutInflater實現子布局的在這個LinearLayout類中顯示。
3.直接在需要使用的xml文件中調用我們寫的這個class,注意路徑要寫全。
1.xml代碼
代碼語言:javascript
<?xml version="1.0" encoding="utf-8"?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="5dp" android:layout_height="match_parent" android:background="#000000" </LinearLayout
2.java代碼
代碼語言:javascript
package com.example.lenovo.myfragmentdemo3; import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout; import com.example.lenovo.myfragmentdemo3.R; /** * Created by lenovo on 2018/5/7. */ public class Linedemo extends LinearLayout { public Linedemo(Context context, @Nullable AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.line,this); }
}
3.布局代碼
代碼語言:javascript
<?xml version="1.0" encoding="utf-8"?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" <com.example.lenovo.myfragmentdemo3.Linedemo android:layout_width="20dp" android:layout_height="match_parent" android:layout_marginRight="100dp"/ <com.example.lenovo.myfragmentdemo3.Linedemo android:layout_width="20dp" android:layout_height="match_parent" android:layout_marginRight="100dp"/ <com.example.lenovo.myfragmentdemo3.Linedemo android:layout_width="20dp" android:layout_height="match_parent" android:layout_marginRight="100dp"/ <com.example.lenovo.myfragmentdemo3.Linedemo android:layout_width="20dp" android:layout_height="match_parent" android:layout_marginRight="100dp"/ </LinearLayout
運行效果:
以上就是本文的全部內容,希望對大家的學習有所幫助。
更多Android進階指南 可以掃碼 解鎖 《Android十大板塊文檔》

1.Android車載應用開發系統學習指南(附項目實戰)
2.Android Framework學習指南,助力成為系統級開發高手
3.2023最新Android中高級面試題匯總+解析,告別零offer
4.企業級Android音視頻開發學習路線+項目實戰(附源碼)
5.Android Jetpack從入門到精通,構建高質量UI界面
6.Flutter技術解析與實戰,跨平臺首要之選
7.Kotlin從入門到實戰,全方面提升架構基礎
8.高級Android插件化與組件化(含實戰教程和源碼)
9.Android 性能優化實戰+360°全方面性能調優
10.Android零基礎入門到精通,高手進階之路
敲代碼不易,關注一下吧。?( ′・?・` ) 🤔