2017-05-29 78 views
0

我有一個按鈕,我的項目的不同部分使用,我需要使用在我的所有項目一樣禮節此相同的按鈕,我想創建一個組件,只是調用此組件,如果我需要改變我的按鈕,改變只有一個班級改變所有用途。撰寫按鈕

我有這個按鈕:

<br.com.simplepass.loading_button_lib.customViews.CircularProgressButton 
      android:id="@+id/createMatch" 
      android:layout_width="120dp" 
      android:layout_height="wrap_content" 
      android:background="@color/buttonColor" 
      android:layout_marginTop="16dp" 
      app:spinning_bar_width="4dp" 
      app:spinning_bar_color="#FFF" 
      android:alpha="0.5" 
      android:textColor="@color/colorAccent" 
      android:elevation="4dp" 
      android:enabled="false" 
      android:layout_marginLeft="16dp" 
      android:textStyle="bold" 
      android:text="@string/createMatch" 
      android:textSize="14sp" 
      app:spinning_bar_padding="6dp"/> 

創建擴展CircularProgressButton一類,但有些禮節我沒有IDEIA如何在代碼訪問。例如,app的特性:

合併兩個問題,如何使一個組件與各種itens佈局?

例如:

<LinearLayout> 
    <Toolbar/> 
    <LinearLayout/> 
</LinearLayout> 

我想這個組合中的XML類調用,並得到這一切itens使用我的自定義類的所有佈局。

<MyLinearLayout> 
... 
</MyLinearLayout> 

回答

1

一些禮節我沒有IDEIA如何在代碼訪問。例如,app的特性:

Have a look here. 你將不得不繼承視圖,並提供一個構造函數上下文和的AttributeSet。

class CircleView extends View { 

    private String mTitleText = "Your default title"; 

    public CircleView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     // You can and should also pass a style as third argument 
     final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CircleView, 0, 0); 

     if (a.hasValue(R.styleable.CircleView_titleText)) { 
      mTitleText = a.getString(R.styleable.CircleView_titleText); 
     } 

     a.recycle(); 
    } 
} 

你可能注意到在我的例子中使用了R.styleable.CircleView_titleText。在使用自定義屬性之前,您必須告訴編譯器關於它們的信息。這是通過添加.xml來定義您的屬性及其預期格式來實現的。

<resources> 
    <declare-styleable name="CircleView"> 
     <attr name="titleText" format="string" /> 
    </declare-styleable> 
</resources> 

怎麼我就與各種itens佈局的組成部分?

Reusable Layouts可能是你在找什麼。

例如,將自定義工具欄定義爲toolbar.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#e40056" 
    android:elevation="4dp" 
    android:minHeight="?attr/actionBarSize" 
    app:titleTextColor="#ffffff" 
    /> 

,然後include它在其他觀點:

<include 
    android:id="@+id/myToolbarId" 
    layout="@layout/toolbar" 
    /> 
+0

感謝完整的答案,我就來看! –

+0

很高興能幫到您:) 如果您發現我的回答有幫助,請將其標記爲已接受的答案。 – finngu

0

如果你看一看爲ProgressBar的源代碼,你會看到,你可以用一個TypedArray檢索從樣式屬性XML AttributeSet

public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 
    ... 
    final TypedArray a = context.obtainStyledAttributes(
      attrs, R.styleable.ProgressBar, defStyleAttr, defStyleRes); 
    ... 
    // this is where we get the drawable that is specified in the XML 
    final Drawable progressDrawable = a.getDrawable(R.styleable.ProgressBar_progressDrawable); 
    ... 
} 

你可以做同樣得到app屬性你正在尋找,但某些屬性被標記爲內部。在這些情況下,我會考慮在res/values/attrs.xml中覆蓋它們,因爲它們可能會改變。

注:progressDrawable不一定是內部的,我只是用它作爲一個例子

<resources> 
    <declare-styleable name="ProgressBar"> 
     <attr name="progressDrawable"/> 
    </declare-styleable> 
</resources> 

關於你的第二個問題,我不知道,如果你想擴展LinearLayout(我由於您必須編寫代碼來創建視圖),或者只是重複使用其中包含視圖的LinearLayout,因此會提出建議。

如果是後者,那麼你可以製作一個XML文件,其中只有你的LinearLayout裏面有適當的孩子,我們稱之爲myLayout.xml。然後,在你其他的XML文件你可以把它放在像這樣:

myLayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout> 
    <Toolbar/> 
    <LinearLayout/> 
</LinearLayout> 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/lorem_ipsum"/> 

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

</LinearLayout> 
+0

謝謝!我會看一看 –