【问题标题】:get parent's view from a layout从布局中获取父视图
【发布时间】:2013-07-26 15:04:57
【问题描述】:

我有一个FragmentActivity,这个布局:

<?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>

ScrollerLinearLayout 类是这样的:

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

如何从ScrollerLinearLayout 访问父布局以获取 - 例如 - editetxt? FragmentActivityScrollerLineraLayout 位于同一项目的不同包中。 我尝试在ScrollerLinearLayout 中以这种方式使用getParent() 函数,但它不起作用。

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

有人帮助我吗?谢谢!

【问题讨论】:

  • 究竟是什么不起作用?
  • @FD_ 它是RelativeLayout r = (RelativeLayout) ((ViewGroup) this.getParent()).getParent(); 上的空指针异常
  • ScrollerLinearLayout 在哪里调用此代码?
  • @FD_ 在方法init()
  • 在调用init() 之前是否将其添加到ViewGroup 中?

标签: android android-layout view parent-child


【解决方案1】:

getParent 方法返回 ViewParent,而不是 View。您还需要将第一个电话发送给getParent()

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

正如 OP 在 cmets 中所述,这会导致 NPE。要调试,请将其拆分为多个部分:

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.

【讨论】:

  • 我试图这样做,但它不起作用。错误是 RelativeLayout r = (RelativeLayout) ((ViewGroup) this.getParent()).getParent(); 上的 nullpointexception 我不明白可能是什么......
  • @Rajin,我在帖子中添加了一部分来帮助您调试此问题。
  • 我检查了日志,发现ViewParent parent = this.getParent(); 返回空值。我有这个public class ScrollerLinearLayout extends LinearLayout { RelativeLayout r; // some code public void init(){ // your code } } 在我调用getParent() 方法的地方是正确的吗?
【解决方案2】:

如果您想从您的 Fragment 中找到 View,请尝试这样做:

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

【讨论】:

  • 对于扩展 LinearLayout 类的 ScrollerLineraLayout 类型,未定义 getActivity() 方法
【解决方案3】:

RelativeLayout(即ViewParent)应具有在布局文件中定义的资源 ID(例如,android:id=@+id/myParentViewId)。如果您不这样做,对 getId 的调用将返回 null。更多信息请查看this answer

【讨论】:

    【解决方案4】:

    您可以使用以下代码获得ANY视图

    view.rootView.findViewById(R.id.*name_of_the_view*)

    编辑:这适用于 Kotlin。在 Java 中,您可能需要这样做=

    this.getCurrentFocus().getRootView().findViewById(R.id.*name_of_the_view*);

    我从 @JFreeman 的回答中学到了 getCurrentFocus() 函数

    【讨论】:

      【解决方案5】:

      这也有效:

      this.getCurrentFocus()
      

      它获取视图,因此我可以使用它。

      【讨论】:

        【解决方案6】:

        查看我的答案here

        Layout Inspector工具的使用在你有一个复杂的视图或者你正在使用第三方库而你不能给视图添加一个id的时候会非常方便

        【讨论】:

          【解决方案7】:

          随便写

          this.getRootView();
          

          【讨论】:

          • 这只是返回育儿视图而不是像一级父级那样的实际布局
          猜你喜欢
          • 2012-06-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-25
          • 1970-01-01
          • 2016-03-15
          • 1970-01-01
          相关资源
          最近更新 更多