2017-09-19 20 views
0

我有一大堆表單控件,所有顯示一個圖標到左側,像這樣的:定製的LinearLayout與圖標

enter image description here

爲此,我有這樣的代碼:

<LinearLayout 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="8dp" 
        android:layout_weight="1" 
        android:orientation="horizontal"> 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="@dimen/icon_edittext_height" 
         android:layout_gravity="top" 
         android:layout_marginRight="@dimen/icon_edittext_marginRight" 
         android:layout_marginTop="@dimen/icon_edittext_margintop" 
         android:src="@drawable/ic_location_city_black_24dp"/> 

        <FrameLayout 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"> 

         <android.support.design.widget.TextInputLayout 
          android:id="@+id/create.data.city.layout" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          > 
          <AutoCompleteTextView 
           android:id="@+id/create.data.city" 
           android:layout_width="match_parent" 
           android:layout_height="wrap_content" 
           android:hint="@string/create_event_city" 
           android:imeOptions="actionNext" 
           android:inputType="textCapSentences" 
           android:maxLines="1" 
           /> 

         </android.support.design.widget.TextInputLayout> 

        </FrameLayout> 

       </LinearLayout> 

現在因爲我有大約20個控件,我不想重複:

<LinearLayout> 
    <ImageView/> 
    <FrameLayout> 
     ...content.... 
    </FrameLayout> 
</LinearLayout> 

模式20次,但我願做這樣的事情:

<MycustomLayout> 
    <EditText .../> 
</MycustomLayout> 

<MycustomLayout> 
    <Spinner ... /> 
</MycustomLayout> 

所以基本上我想創建一個擴展線性自定義佈局,我希望能夠在我使用的LinearLayout以同樣的方式使用它,同時在任何時候都有圖標。

+0

好!你在編寫自定義佈局的時候是什麼階段? – azizbekian

+0

我創建了一個擴展LinearLayout,放置一個圖像,但不知道如何使內的內容進入內部FrameLayout – MichelReap

回答

0

您可以嘗試在佈局中使用包含標籤以包含重複的部分。或者,如果不工作,你可以嘗試這樣的事:

public class CustomControl extends LinearLayout { 

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

     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     inflater.inflate(R.layout.custom_layout, this); 
    } 
} 

哪裏custom_layout是你的自定義視圖的佈局。 然後,你可以做你的XML:

<CustomControl /> 

UPD: 爲了增加孩子的意見在XML只是覆蓋onFinishInflate並添加觀點是這樣的:

protected void onFinishInflate() {  
     View[] children = detachChildren(); 
     for (int i = 0; i < children.length; i++) 
      this.addView(children[i]); 
     } 
+0

這不回答我的問題 – MichelReap

+0

更新爲添加子視圖 – kodlan

0

無需添加太多佈局。只是嘗試使用這個......

<android.support.design.widget.TextInputLayout 
    android:id="@+id/create.data.city.layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <AutoCompleteTextView 
     android:id="@+id/create.data.city" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="City" 
     android:drawableLeft="@drawable/ic_location_city_black_24dp" 
     android:drawablePadding="5dp" 
     android:imeOptions="actionNext" 
     android:inputType="textCapSentences" 
     android:maxLines="1" /> 

</android.support.design.widget.TextInputLayout> 
+0

這並沒有達到相同的結果 – MichelReap

0

解決它自己,像這樣:

public class IconFieldLinearLayout extends LinearLayout { 


    private Drawable mDefaultDrawable; 

    public IconFieldLinearLayout(Context context) { 
     super(context); 
     init(context, null); 
    } 

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

    public IconFieldLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
     init(context, attrs); 
    } 

    @TargetApi(Build.VERSION_CODES.LOLLIPOP) 
    public IconFieldLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
     init(context, attrs); 
    } 

    private void init(Context context, AttributeSet attrs) { 
     this.setOrientation(HORIZONTAL); 
     mDefaultDrawable = getResources().getDrawable(R.drawable.tv_avatar_default); 
     initAttr(context, attrs); 
    } 

    private void initAttr(Context context, AttributeSet attrs) { 

     if (attrs != null) { 
      TypedArray a = context.obtainStyledAttributes(attrs, 
        R.styleable.IconFieldLinearLayout, 0, 0); 
      Drawable icon = a.getDrawable(R.styleable.IconFieldLinearLayout_icon); 
      a.recycle(); 
      initIcon(context, icon); 
     } 
     else { 
      initIcon(context, mDefaultDrawable); 
     } 
    } 

    private void initIcon(Context context, Drawable icon) { 
     ImageView imageView = new ImageView(context); 
     imageView.setColorFilter(ContextCompat.getColor(context, R.color.colorAccent)); 
     int dimension = getResources().getDimensionPixelSize(R.dimen.icon_edittext_height); 
     int marginRight = getResources().getDimensionPixelSize(R.dimen.icon_edittext_marginRight); 
     int marginTop = getResources().getDimensionPixelSize(R.dimen.icon_edittext_margintop); 
     LinearLayout.LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, dimension); 
     layoutParams.gravity = Gravity.TOP; 
     layoutParams.setMargins(0, marginTop, marginRight, 0); 
     imageView.setImageDrawable(icon); 
     addView(imageView, layoutParams); 
    } 


} 

並不真的需要把FrameLayout裏在那裏。