AndroidレイアウトXML属性が拡張されました(角が丸い、点線、境界線など)。

概観

          Androidの開発では、レイアウトファイルの作成にxmlを使用することがよくありますが、この実装は単純であるだけでなく、表現力にも優れています。ただし、Googleが提供するレイアウト属性は制限されており、一部の機能を実現するにはコードまたはカスタムコントロールを使用する必要があります。追加機能を実現するために属性を拡張する方法はありますか?たとえば、背景を表現して設定するために背景を使用することがよくありますが、丸い角を表現して設定するためにlayout_radiusを使用できますか?

 

使用例

  プロジェクトbuild.gradleで依存関係を参照する必要がある

 implementation 'com.zhangzheng.superxml:library:1.1.0'

 また、アプリケーションにコード行を登録します

  SuperXml.init(this)

  オーバー

 

機能の説明

 

属性の強化

   フィレット:         

   <View
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="20dp"
        android:background="#FF0000"
        app:layout_radius="40dp" />

  説明

       app:layout_radiusは、コントロールの背景を丸みを帯びた角に設定することをサポートし、背景は無地の背景または画像をサポートします。さらに、ImageViewのソースを丸みを帯びた角に設定する場合は、たとえばapp:layout_src_radiusを使用する必要があります。 

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:src="@mipmap/order_ic_shipper_default"
    app:layout_src_radius="10dp" />

 

   複合属性

       

  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="asdasdasdasd"
        app:layout_textColor_pressedFalse="#00FF00"
        app:layout_textColor_pressedTrue="#FF0000" />

   説明

        通常の状況では、クリックと通常の状態でフォントの色が異なることを示すか、背景でセレクタを使用してファイルを定義し、それをレイアウトファイルで参照します。一方、このような使用は非常に面倒であり、他方では、読みやすさも低下します。減らす(ユーザーは、コード式の意図を知る前に、分析のためにセレクターファイルを入力する必要があります)。一般的に使用される再利用プロパティは、次のようにここにカプセル化されます。

の属性 属性タイプ 説明
layout_background_enableTrue
layout_background_enableFalse
reference|color(资源或者颜色)
背景(利用可能ですか)
layout_background_pressedTrue
layout_background_pressedFalse
参照|色(リソースまたは色) 背景(押されたかどうか)
layout_background_selectedTrue
layout_background_selectedTrue
参照|色(リソースまたは色) 背景(選択するかしないか)
layout_textColor_enableTrue
layout_textColor_enableFalse
参照|色(リソースまたは色) フォントの色(利用可能)
layout_textColor_pressedTrue
layout_textColor_pressedFalse

 

参照|色(リソースまたは色) フォントの色(押されているかどうかにかかわらず)
layout_textColor_selectedTrue
layout_textColor_selectedFalse
参照|色(リソースまたは色) フォントの色(選択するかどうか)
     

 

フレーム   

 <View
        android:id="@+id/view"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="20dp"
        android:background="#FF0000"
        app:layout_border_color="#0000FF"
        app:layout_border_width="1dp"
        app:layout_radius="40dp" />

 

説明

      比較的単純な、layout_border_colorは境界線の色を表し、layout_border_widthは境界線の太さを表し、radiusは一緒に使用されて、境界線の丸い角を表します。

 

 

点線

 <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_marginTop="20dp"
        android:background="#FF0000"
        app:layout_dash_gap="10dp"
        app:layout_dash_height="1dp"
        app:layout_dash_width="5dp" />

 

説明

     任意のビューで使用でき(ビューで定義することをお勧めします)、grap、dash_height、およびdash_widthは同時に定義する必要があります。水平および垂直の点線をサポートします。ここでは、ビューの幅と高さを検出して決定します。プロパティの説明:dash_grap(点線の間隔)、dash_width(単一の小さな線の幅)、dash_height(単一の小さな線の高さ)

 

 

交換または機能強化を表示

          この機能により、レイアウトファイルのビューが他のコントロールに置き換えられたり、強化されたりする場合があります。

 

スクロールビュー

      小さな画面の携帯電話に適応するために、各レイアウトファイルにScrollViewのレイヤーを追加し、コンテナーコントロールを拡張します。

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="130dp"
        android:gravity="center"
        android:orientation="vertical"
        android:scrollbars="none"
        app:layout_canScroll="true">

説明

     スクロール機能を持​​たせるためにスクロールする必要があるビューに、属性app:layout_canScroll = "true"を追加します。さらに、すべてのscrollViewプロパティは、コンテナーコントロールで構成できます。

 

 

属性オーバーライド

          非常に一般的なビジネスシナリオがあります。アイテムには複数のコントロールがあります。コントロールのほとんどの属性は同じです(TextViewのフォントの色やサイズなど)。通常、各コントロールに同じ属性を追加します(冗長) )、またはパブリックスタイルを定義する(面倒すぎる)。次に、htmlのレイアウトを参照し、親コントロールで共通のスタイルを設定し、子コントロールのデフォルト値を設定します。

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:textColor="#FF0000"
        android:textSize="20sp"
        android:textStyle="italic"
        android:scaleType="center"
        app:layout_cover_children="true">

        <ImageView

            android:layout_width="50dp"
            android:src="@mipmap/ic_launcher"
            android:layout_height="50dp"/>

        <TextView
            android:capitalize="none"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FF00FF"
            android:textStyle="italic"
            android:text="11111" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:textSize="13dp"
            android:text="22222" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:textSize="14dp"
            android:text="333333" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:textSize="15dp"
            android:text="444444" />

    </LinearLayout>

 

サポートされる属性

       

制御タイプ の属性  
TextView
textColor
 
textSize
 
text
 
maxLines
 
maxEms
 
textColorHint
 
hint
 
textDirection
 
textStyle
 
capitalize
 
ImageView
src
 
scaleType
 

 

拡大する

 

プロパティ拡張

  SuperXml.addDecorate(object : IDecorateView() {
            override fun initExtraInfo(typedArray: TypedArray): Boolean {
            }

            override fun decorate(view: View) {
            }
        })

実装リファレンス

internal class RadiusDecorate(var radius: Float = 0f) : IDecorateView() {

    override fun initExtraInfo(typedArray: TypedArray): Boolean {
        radius = typedArray.getDimension(R.styleable.decorate_view_layout_radius,0f)
        return radius > 0
    }

    override fun decorate(view: View)= view.setRadius(radius)

}

 

置換または強化の制御

 SuperXml.addDecorate(object :IWrapDecorateView(){
            override fun decorateView(view: View): View {
            }

            override fun initExtraInfo(typedArray: TypedArray): Boolean {
            }
        })

実装リファレンス

internal class ScrollWrapDecorate(var canScroll: Boolean = false) : IWrapDecorateView() {

    override fun initExtraInfo(typedArray: TypedArray): Boolean {
        canScroll = typedArray.getBoolean(R.styleable.decorate_view_layout_canScroll, false)
        return canScroll
    }

    override fun decorateView(view: View): View {
        return ScrollViewProxy(
            view,
            attributeSet
        )
    }


}

 

属性オーバーライド

  SuperXml.addCoverAttributeParse(object : AbsChildViewParse<TextView>(){
            override fun createInfoView(context: Context, attributeSet: AttributeSet?): TextView {
            }

            override fun coverAttribute(): MutableList<*> {
            }
        })

実装リファレンス

class TextViewCoverParse : AbsChildViewParse<TextView>() {

    override fun createInfoView(context: Context, attributeSet: AttributeSet?): TextView =
        TextView(context, attributeSet)

    override fun coverAttribute(): MutableList<*> = mutableListOf(
        AttributeInfo("textSize",{ textSize }) { value -> textSize = value },
        AttributeInfo("textColor",{ textColors }) { value -> setTextColor(value) },
        AttributeInfo("text",{ text }) { text -> setText(text) },
        AttributeInfo("maxLines",{ maxLines }) { maxLines -> setMaxLines(maxLines) },
        AttributeInfo("maxEms",{ maxEms }) { maxEms -> setMaxEms(maxEms) },
        AttributeInfo("textColorHint",{ hintTextColors }) { hintTextColors -> setHintTextColor(hintTextColors) },
        AttributeInfo("hint",{ hint }) { hint -> setHint(hint) },
        AttributeInfo("textDirection",{ textDirection }) { textDirection -> setTextDirection(textDirection) },
        AttributeInfo("textStyle",{ typeface }) { typeface -> setTypeface(typeface) },
        AttributeInfo("capitalize",{ inputType }) { inputType -> setInputType(inputType) }
    )



}

 

コード:github

https://github.com/long8313002/SuperXml

おすすめ

転載: blog.csdn.net/long8313002/article/details/108703057