2012-05-08 35 views
6

Tôi đang cố gắng tạo chế độ xem tùy chỉnh của mình thông qua xml, nhưng màn hình không hiển thị chế độ xem của tôi, vì nó không tăng cao. My xem tùy chỉnh xml là như:Chế độ xem tùy chỉnh không tăng thêm

<?xml version="1.0" encoding="utf-8"?> 
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/profileSwitcher" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF" > 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <ImageView 
      android:id="@+id/imgAdvertise" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:src="@android:drawable/btn_minus" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" > 

     <TextView 
      android:id="@+id/txtAdvertise" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:text="asdas" /> 
    </LinearLayout> 

</ViewSwitcher> 

Tôi có lớp tùy chỉnh View tôi như:

public class MyCustomView extends View{ 

    TextView textAdvertise; 
    ImageView imageAdvertise; 
    View view; 
    ViewSwitcher switcher ; 

    public MyCustomView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 

     LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (layoutInflater != null) { 
      view = layoutInflater.inflate(R.layout.main, null); 

     } 

     initialize(); 
    } 

    public void initialize() { 
     // TODO Auto-generated method stub 

     textAdvertise = (TextView) view.findViewById(R.id.txtAdvertise); 
     imageAdvertise = (ImageView) view.findViewById(R.id.imgAdvertise); 
     switcher = (ViewSwitcher) view.findViewById(R.id.profileSwitcher); 
     startAnimation(); 

    } 

    public void startAnimation() { 
     // TODO Auto-generated method stub 

     new Thread() { 
      public void run() { 

       for (;;) { 
        try { 

         Thread.sleep(2000); 
         hRefresh.sendEmptyMessage(5); 
        } catch (Exception e) { 
        } 
       } 
      } 
     }.start(); 
    } 

    Handler hRefresh = new Handler() { 
     public void handleMessage(Message msg) { 
      switch (msg.what) { 
      case 5: 
       switcher.showNext(); 
       // To go back to the first view, use switcher.showPrevious() 
       break; 
      default: 
       break; 
      } 
     } 
    }; 

} 

và trong cách bố trí xml cơ sở của tôi, nơi tôi có để hiển thị các View, là một nút và và View của tôi như:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<Button 
    android:id="@+id/button1" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:text="Button" 
    android:layout_weight="1"/> 

<com.example.viewswitcher.MyCustomView 
    android:id="@+id/MyView" 
    android:layout_width="fill_parent" 
    android:layout_height="0dp" 
    android:layout_weight="1" 

    /> 

Can ai xin vui lòng cho tôi biết những gì nó đang xảy ra?

Trả lời

9

Bạn sẽ không thấy gì bởi vì MyCustomView không có bất kỳ nội dung nào như bạn đã viết. Trước hết tôi sẽ sử dụng một trong những ViewGroups trẻ em thay vì View (như LinearLayout, RelativeLayout vv):

public class MyCustomView extends LinearLayout { 

    TextView textAdvertise; 
    ImageView imageAdvertise; 
    View view; 
    ViewSwitcher switcher ; 

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

    public MyCustomView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (layoutInflater != null) { 
      view = layoutInflater.inflate(R.layout.main, this, true); 
     } 
     initialize(); 
    } 

    public MyCustomView(Context context, AttributeSet attrs, int theme) { 
     super(context, attrs, theme); 
    } 
// ... 
+1

Cảm ơn bạn đã trợ giúp .. Nó hoạt động .. Thực ra trong tôi bố trí inflator tôi đã viết xem = layoutInflater.inflate (R.layout.main, null); null này gây ra vấn đề .. thay vào đó tôi đặt "này" Nó làm việc .. cảm ơn bạn thân !! – NullPointerException

+0

chỉnh sửa bạn đã thực hiện trong câu hỏi ... và một điều nữa nó hoạt động vì tôi đã mở rộng LinearLayout thay vì xem .. tại sao điều này như vậy ?? Mọi liên kết hoặc bài đăng về chi tiết này .. cảm ơn – NullPointerException

+2

@Rashmi Tôi đã chỉnh sửa câu hỏi của bạn để định dạng mã của câu hỏi tốt hơn. Lớp 'View' đại diện cho một đối tượng' View' duy nhất, nó không chấp nhận '' '' '' '' '' '' '' '' '' '' '' '' '' '' (thực tế nếu bạn nhìn vào phương thức 'inflate' tài liệu bạn sẽ thấy rằng tham số thứ hai là một 'ViewGroup'). Thay vào đó một 'ViewGroup' (' LinearLayout' là một phân lớp của 'ViewGroup') đại diện cho một' View' (trên thực tế nó là một phân lớp của lớp siêu 'View') chấp nhận trẻ' Views'. Tôi không biết bất kỳ liên kết nào nhưng bạn có thể xem các tài liệu cho 'View' và' ViewGroup'. – Luksprog

0

Tôi nghĩ rằng những gì bạn cần không phải là một tùy chỉnh View. Có lẽ bạn nên kế thừa từ lớp Fragment insted of View.

Nhìn here để biết cách triển khai Phân đoạn riêng.

+0

Xin lỗi các đoạn không hữu ích trong trường hợp của tôi .. vì tôi phải cung cấp khả năng tương thích với ứng dụng của tôi từ Android 2.1 – NullPointerException

+0

Bạn có thể sử dụng Gói hỗ trợ Android. [link] (http://developer.android.com/sdk/compatibility-library.html). Tối thiểu versión là 4 (1,6, tôi nghĩ). – gutiory

3

Sử dụng quyền này để tăng bố cục của bạn. Sửa

View headerView = View.inflate(this, R.layout.layout_name, null); 

này sẽ trả lại bố trí mẹ của tập tin xml của bạn sau đó bạn có thể trực tiếp sử dụng nó hoặc bạn có thể tìm thấy bất kỳ điểm khác bằng cách sử dụng

Sửa

headerView.findViewById(R.id.view_id); 

hy vọng nó sẽ hoạt động

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