Android之視圖

一、簡介

  • 所有的可視化控件都是繼承自Android.view.View
  • 創(chuàng)建方式 第一種xml創(chuàng)建,第二種Java代碼創(chuàng)建
  • 長度單位 一般寫在dimens文件中,通過引用使用
單位 說明
px 屏幕實(shí)際像素,如320*480
dp(dip) 屏幕的物理尺寸,大小為1英寸的1/72
sp 與dp類似,根據(jù)用戶的字體大小首選項(xiàng)進(jìn)行的縮放
  • 布局共有5種布局
    LinearLayout 線性布局 分為垂直和水平方向
    RelativeLayout 相對布局
    TableLayout 表格布局
    FrameLayout 框架布局 視圖可以重疊
    AbsoluteLayout 絕對布局 控件通過坐標(biāo)放置

二、布局

  • weight 權(quán)重,控件占的份數(shù),如果容器中的控件權(quán)重都相同,則表示均分
  • orientation 對于LinearLayout,排列方向,參數(shù)vertical和 horizontal
  • margin 控件在容器中相距邊的距離
  • visibility 視圖可見設(shè)置,三個(gè)值,visible、invisible、gone(把控件視圖直接移除了)
  • gravity 調(diào)整顯示方式,居中啥的
  • layout_x、layout_y AbsoluteLayout指定坐標(biāo)
  • layout_below、layout_above、layout_toLeftOf、layout_toRightOf RelativeLayout設(shè)置
  • padding 控件上下左右距離

三、View 和ViewGroup

  • Android 的UI 界面都是由 View 和ViewGroup及其派生類組合而成
  • View是所有UI組件的基類,而ViewGroup是容納這些組件的容器。其本身也是從View派生出來的
  • View派生出的直接子類:
    AnalogClock
    ImageView
    KeyboardView
    ProgressBar
    SurfaceView
    TextView
    ViewGroup
    ViewStub
  • ViewGroup派生出的直接子類:
    AbsoluteLayout
    AdapterView
    FragmentBreadCrumbs
    FrameLayout
    LinearLayout
    RelativeLayout
    SlidingDrawer
  • 靜態(tài)動態(tài)加載布局
    include
    ViewStup

四、自定義控件

  • View是Android最基本的一種UI組件
  • ViewGroup是一種特殊的View,可以包含多個(gè)子View和子ViewGroup
  • 所有控件都直接或者間接繼承自View
  • 所有布局都直接或間接繼承自ViewGroup
  • 直接引入布局有兩種方式include和ViewStup
  • 當(dāng)布局中有相應(yīng)事件時(shí),就需要自定義控件

4.1 引入布局

定義一個(gè)布局title

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

    <Button
        android:id="@+id/btn_return"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="return"
        android:layout_weight="1"/>

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Title"
        android:layout_weight="1"/>

    <Button
        android:id="@+id/btn_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Edit"
        android:layout_weight="1"/>

</LinearLayout>

引入

  <include layout="@layout/title"/>

4.2 自定義控件

創(chuàng)建一個(gè)繼承View或者View子類的類

public class TitleLayout extends LinearLayout {

    private static final String TAG = "TitleLayout";

    public TitleLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.title, this);

        Button btn_return = findViewById(R.id.btn_return);
        Button btn_edit = findViewById(R.id.btn_edit);
        btn_edit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "onClick: Edit");
            }
        });

        btn_return.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                ((Activity)getContext()).finish();
            }
        });
    }
}

使用自定義控件

   <com.example.jony.customdemo1.TitleLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

注1:LayoutInflater.from(context).inflate(R.layout.title, this);
通過LayoutInflater的from()方法構(gòu)建一個(gè)LayoutInflater對象,該對象調(diào)用inflate()方法動態(tài)加載一個(gè)布局文件,inflate()方法兩個(gè)參數(shù),第一個(gè)布局文件id,第二個(gè)參數(shù)是給加載好的布局再添加一個(gè)父布局
注2: ((Activity)getContext()).finish();
獲得所在的上下文,并銷毀,此例是直接在activity_main,所以點(diǎn)擊就直接銷毀主Activity,程序退出

帶有自定義屬性的自定義控件
自定義

public class CustomView extends View {

    private boolean showtext;
    private int labelPosition;

    public CustomView(Context context) {
        super(context);

    }

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

       TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomView, 0,0);

        try {
            showtext = a.getBoolean(R.styleable.CustomView_showText, false);
            labelPosition = a.getInt(R.styleable.CustomView_labelPosition, 0);
        }finally {
            a.recycle();
        }

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        setBackgroundColor(Color.RED);
        super.onDraw(canvas);
    }
}

在values文件夾下創(chuàng)建attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomView">
        <attr name="showText" format="boolean" />
        <attr name="labelPosition" format="enum">
            <enum name="left" value="0"/>
            <enum name="right" value="1"/>
        </attr>
    </declare-styleable>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    xmlns:custome="http://schemas.android.com/apk/com.example.jony.customdemo1"
    tools:context="com.example.jony.customdemo1.MainActivity">

    <!--使用自定義控件-->
    <com.example.jony.customdemo1.CustomView
        android:layout_width="100dp"
        android:layout_height="20dp"
        custome:showText="true"
        custome:labelPosition="left"/>

</android.support.constraint.ConstraintLayout>

注:xmlns:custome="http://schemas.android.com/apk/com.example.jony.customdemo1"
自定義的命名空間,最后是應(yīng)用的包名

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容