2013-07-26 17 views
28

Tôi có một FragmentActivity với cách bố trí này:có được cái nhìn của cha mẹ từ một bố cục

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 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" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/menulabel" 
    android:textSize="24sp" /> 

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:ems="10" 
    android:inputType="text" 
    android:layout_below="@+id/textView1" 
    android:hint="@string/search_title_hint" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/spinnersearch" 
    android:layout_below="@+id/editText1" /> 

<LinearLayout 
    android:id="@+id/layout1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView2" > 

    <Spinner 
     android:id="@+id/spinner_search" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:prompt="@string/spinnersearch" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/reset_search" /> 

</LinearLayout> 

<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/buttonnewcat" 
    android:layout_below="@+id/layout1" /> 

<Button 
    android:id="@+id/button3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/buttondelcat" 
    android:layout_below="@+id/button2" /> 

<Button 
    android:id="@+id/button4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/buttoneditcat" 
    android:layout_below="@+id/button3" /> 

<Button 
    android:id="@+id/button5" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/buttonnewtext" 
    android:layout_below="@+id/button4" /> 

<Button 
    android:id="@+id/button6" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/buttondeltext" 
    android:layout_below="@+id/button5" /> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <side.menu.scroll.ScrollerLinearLayout 
     android:id="@+id/menu_content_side_slide_layout" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="45dp" 
      android:background="@drawable/ab_solid_example" 
      android:orientation="horizontal" > 

      <ImageView 
       android:id="@+id/main_tmp_button" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:background="@drawable/actionbar_background" 
       android:src="@drawable/download" /> 

     </LinearLayout> 
     <FrameLayout 
      android:id="@+id/fragment_container" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@drawable/bg_groud" /> 

    </side.menu.scroll.ScrollerLinearLayout> 
</RelativeLayout> 

và lớp ScrollerLinearLayout là một cái gì đó như thế này:

public class ScrollerLinearLayout extends LinearLayout { 
    // code of the class 
} 

Làm thế nào để truy cập từ ScrollerLinearLayout để bố cục gốc để lấy - ví dụ - editetxt? FragmentActivityScrollerLineraLayout nằm trong các gói khác nhau của cùng một dự án. Tôi đã thử sử dụng chức năng getParent() theo cách này trong phạm vi ScrollerLinearLayout nhưng không hoạt động.

RelativeLayout r = (RelativeLayout) this.getParent().getParent(); 
int w = r.findViewById(R.id.editText1).getLayoutParams().width; 

Ai đó giúp tôi? Cảm ơn!

+2

gì chính xác không hoạt động? –

+0

@FD_ nó là một nullpointerexception trên 'RelativeLayout r = (RelativeLayout) ((ViewGroup) this.getParent()). GetParent();' –

+0

Nơi trong 'ScrollerLinearLayout' bạn có gọi mã này? –

Trả lời

59

Phương thức getParent trả lại ViewParent, không phải là View. Bạn cần phải cast cuộc gọi đầu tiên để getParent() thêm:

RelativeLayout r = (RelativeLayout) ((ViewGroup) this.getParent()).getParent(); 

Như đã trình bày trong các ý kiến ​​của OP, điều này đang gây ra một NPE. Để gỡ rối, chia này ra thành nhiều phần:

ViewParent parent = this.getParent(); 
RelativeLayout r; 
if (parent == null) { 
    Log.d("TEST", "this.getParent() is null"); 
} 
else { 
    if (parent instanceof ViewGroup) { 
     ViewParent grandparent = ((ViewGroup) parent).getParent(); 
     if (grandparent == null) { 
      Log.d("TEST", "((ViewGroup) this.getParent()).getParent() is null"); 
     } 
     else { 
      if (parent instanceof RelativeLayout) { 
       r = (RelativeLayout) grandparent; 
      } 
      else { 
       Log.d("TEST", "((ViewGroup) this.getParent()).getParent() is not a RelativeLayout"); 
      } 
     } 
    } 
    else { 
     Log.d("TEST", "this.getParent() is not a ViewGroup"); 
    } 
} 

//now r is set to the desired RelativeLayout. 
+0

tôi đã cố gắng để làm điều này nhưng nó không hoạt động. lỗi là nullpointexception trên 'RelativeLayout r = (RelativeLayout) ((ViewGroup) this.getParent()) getParent();' tôi không hiểu cái gì có thể là ... –

+0

@Rajin, tôi đã thêm một phần của bài viết của tôi để giúp bạn gỡ lỗi vấn đề này. – Phil

+0

tôi đã kiểm tra nhật ký và tôi thấy rằng 'ViewParent parent = this.getParent();' trả về null. Tôi có lớp công khai này ScrollerLinearLayout mở rộng LinearLayout {RelativeLayout r; // một số mã public void init() {// your code}} 'là đúng nơi tôi gọi phương thức' getParent() '? –

4

Nếu bạn đang cố gắng tìm một View từ Fragment của bạn sau đó cố gắng làm việc đó như thế này:

int w = ((EditText)getActivity().findViewById(R.id.editText1)).getLayoutParams().width; 
+0

phương thức 'getActivity()' không được định nghĩa cho kiểu ScrollerLineraLayout, mở rộng lớp LinearLayout –

0

Các RelativeLayout (tức là ViewParent) nên có Id tài nguyên được xác định trong tệp bố cục (ví dụ: android:[email protected]+id/myParentViewId). Nếu bạn không thực hiện điều đó, lệnh gọi hàm getId sẽ trả về null. Hãy xem this answer để biết thêm thông tin.

-2

Chỉ cần viết

this.getRootView(); 
+0

, điều này đơn giản trả về khung nhìn làm cha mẹ chứ không phải bố cục thực tế như chỉ một bậc cha mẹ – Thecarisma

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