2016-01-31 20 views
10

Tôi đang cố tạo chế độ xem phức tạp, nhờ đó tôi có thể đặt thuộc tính trong XML và chuyển chúng đến các trẻ trong chế độ xem kết hợp. Trong đoạn mã bên dưới, tôi muốn đặt android:text và chuyển đến số EditText.Chuyển các thuộc tính sang chế độ xem trẻ em trong Chế độ xem hợp chất

Điều này có thể thực hiện mà không phải đặt mọi thuộc tính làm thuộc tính tùy chỉnh không?

activity.xml:

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 
     <com.app.CustomLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:text="child_view_text" /> 
</FrameLayout> 

custom_view.xml:

<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.design.widget.TextInputLayout 
     android:id="@+id/textInputLayout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <EditText 
      android:id="@+id/editText" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </android.support.design.widget.TextInputLayout> 
</merge> 

CustomView.java:

public ValidationTextInputLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context, attrs); 
} 

private void init(Context context, AttributeSet attrs) { 
    View v = inflate(context, R.layout.custom_view, this); 

    mEditText = (EditText) v.findViewById(R.id.editText); 
    mTextInputLayout = (TextInputLayout) findViewById(R.id.textInputLayout); 
} 

Trả lời

0

Tôi nghĩ có lẽ bạn có thể đặt văn bản tùy chỉnh (ví dụ: child_view_text) trong một chủ đề, sau đó sử dụng chủ đề trên bố cục hoặc chế độ xem gốc. Khi làm điều đó, tất cả các chế độ xem con sẽ có thuộc tính android: text được truyền với văn bản tùy chỉnh.

Trong trường hợp của bạn, nó có thể trông giống như:

<string name="child_view_text">Child View Text</string> 

<style name="CustomTheme" parent="Theme.AppCompat"> 
    <item name="android:text">@string/child_view_text</item> 
</style> 

<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/CustomTheme"/> 
    <com.app.CustomLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
</FrameLayout> 
+0

Điều này không giải quyết được sự cố. Trong một số trường hợp, tôi cần chuyển các thuộc tính tùy chỉnh khác nhau, ví dụ: "app: box_checked = true". – soundsofpolaris

0

com.app.CustomLayout mở rộng với bố cục, có nghĩa là nó không thể lấy android: văn bản như một thuộc tính. Bạn sẽ cần phải tạo một thuộc tính tùy chỉnh cho lớp CustomLayout của bạn và trong lớp CustomLayout đã nói, thổi phồng custom_view của bạn (như bạn đang làm ở trên), phân tích các attrs cho chuỗi văn bản tùy chỉnh của bạn, sau đó gọi mEditText.setText (customLayoutText).

0

Thay vì có các EditText bên CustomLayout.xml bạn có thể nhanh chóng nó trong CustomView.java đi qua các AttributeSet:

public class ValidationTextInputLayout extends LinearLayout 
{ 
    public ValidationTextInputLayout(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     init(context, attrs); 
    } 

    private void init(Context context, AttributeSet attrs) 
    { 
     View v = inflate(context, R.layout.custom_view, this); 

     mTextInputLayout = (TextInputLayout)findViewById(R.id.textInputLayout); 

     mEditText = new EditText(context, attrs); 
     mTextInputLayout.AddView(mEditText); 
    } 

    /* Properties etc... */ 
} 

Đối với ai vẫn còn có vấn đề này.

+0

Ngôn ngữ này được viết bằng ngôn ngữ nào? – azizbekian

+0

Tôi đã viết nó trong java nhưng bản thân tôi sử dụng C# và Xamarin vì vậy đã không có thời gian để kiểm tra nó biên dịch. Tôi đã chỉnh sửa ví dụ của mình để sửa lỗi cú pháp. –

Các vấn đề liên quan