2010-03-09 33 views
34

ok,xem tùy chỉnh với bố trí

những gì tôi đang cố gắng làm là để nhúng một cái nhìn tùy chỉnh trong main.xml bố trí mặc định:

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

    <com.lam.customview.CustomDisplayView 
     android:id="@+id/custom_display_view1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 


    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <Button 
      android:id="@+id/prev" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="50" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="@string/prev" /> 
    </LinearLayout> 
</LinearLayout> 

như bạn có thể nhìn thấy lớp được gọi là com. lam.customview.CustomDisplayView, với id của custom_display_view1.

bây giờ trong lớp com.lam.customview.CustomDisplayView, tôi muốn sử dụng bố cục khác được gọi là custom_display_view.xml vì tôi không muốn tạo các điều khiển/tiện ích theo chương trình.

custom_display_view.xml chỉ là một nút và một hình ảnh, nội dung trong đó tôi muốn thay đổi dựa trên một số điều kiện:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TextView 
    android:id="@+id/display_text_view1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello" 
    /> 
    <ImageView 
    android:id="@+id/display_image_view1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    </ImageView> 

</LinearLayout> 

tôi đã cố gắng để làm:

1)

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

    try 
    { 
     // register our interest in hearing about changes to our surface 
     SurfaceHolder holder = getHolder(); 
     holder.addCallback(this); 

     View.inflate(context, R.layout.custom_display_view, null); 

...

nhưng có lỗi này, "03-08 20: 33: 15.711: ERROR/onCreate (10879): Dòng tệp XML nhị phân # 8: Lỗi khi tăng cấp lớp java.lang.reflect.Constructor ".

2)

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

    try 
    { 
     // register our interest in hearing about changes to our surface 
     SurfaceHolder holder = getHolder(); 
     holder.addCallback(this); 

     View.inflate(context, R.id.custom_display_view1, null); 

...

nhưng đã nhận lỗi này ", 03-08 20: 28: 47,401: ERROR/CustomDisplayView (10806): Resource ID # 0x7f050002 loại # 0x12 không hợp lệ "

cũng vậy, nếu tôi làm theo cách này, như ai đó đã đề xuất, không rõ với tôi như thế nào e custom_display_view.xml được liên kết với lớp chế độ xem tùy chỉnh.

cảm ơn.

+0

Ngôn ngữ? Khung? –

+0

hi i do có cùng một vấn đề ... u có giải pháp nào ??? – Ads

Trả lời

0

Hãy thử sử dụng

context.getLayoutInflater().inflate(R.id.custom_display_view1, null); 
7

Bạn có thể thổi phồng các cutom_display_view vào lớp tùy chỉnh của bạn bằng cách:

public CustomDisplayView(Context context, AttributeSet attrs) { 
    super(context, attrs);   
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      if(inflater != null){  
       inflater.inflate(R.layout.custom_display_view, this); 
      } 
} 
+12

Điều này sạch hơn nhiều: View.inflate (context, R.layout.myactionbar, this); – Ian

70

tôi đã có vấn đề chính xác giống nhau, và vấn đề là tôi đã sử dụng ID sai. .. và nó trông giống như bạn quá.

Bạn nên tham khảo

R.layout.custom_display_view

-not-

R.id.custom_display_view

+2

Câu trả lời hoàn hảo, tôi đã nhầm lẫn với vấn đề này và điều này thực sự đã giúp –

0

Line # 8 là giao diện tùy chỉnh của bạn trong bố trí đầu tiên. Bạn đang cố gắng tải bố trí sai, vì vậy nó đang cố gắng tải chính nó đệ quy?(Tôi chỉ làm điều tương tự)

cũng bạn có:

R.layout.custom_display_view 

vs

R.id.custom_display_view1 

với vào cuối đó ... cái nào là nó?

4

Khi bạn thổi phồng bố cục của mình, bạn nên chuyển tham chiếu đến cá thể chế độ xem để xác định nó là gốc. Vì vậy, thay vì gọi:

View.inflate(context, R.layout.custom_display_view, null); 

Gọi:

View.inflate(context, R.layout.custom_display_view, this); 

Xem: The docs bài

+0

Bạn có thể cung cấp thêm mã để hiển thị cách hoạt động của mã này không? –

+0

@Frank, mã này tương tự như mã OP. –

+0

Điều này chỉ hoạt động nếu lớp tùy chỉnh là một nhóm xem – smac89

7

Blog này đã giúp tôi hiểu phải làm gì vô cùng http://trickyandroid.com/protip-inflating-layout-for-your-custom-view/. Trong trường hợp các bài đăng blog biến mất, đây là một số bộ phận của các mã:

public class Card extends RelativeLayout { 
    public Card(Context context) { 
     super(context); 
     init(); 
    } 

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

    public Card(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    private void init() { 
     inflate(getContext(), R.layout.card, this); 
    } 
} 

với cách bố trí này:

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

<merge xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:padding="@dimen/card_padding" 
    android:background="@color/card_background"> 

    <ImageView 
     ... /> 

    <TextView 
     ... /> 

</merge> 

Việc kiểm soát được bao gồm như:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin"> 

    <com.trickyandroid.customview.app.view.Card 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@color/card_background" 
     android:padding="@dimen/card_padding"/> 

</FrameLayout> 

đâu com.trickyandroid .customview.app.view là vùng tên của thẻ lớp. Một điều mới mẻ đối với tôi là thẻ "hợp nhất", kết thúc bằng cùng một nút với thẻ Thẻ trong tài liệu chứa.

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