从一个布局中获取父视图

59

我有一个带有以下布局的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访问其父布局以获取-例如-EditText?FragmentActivityScrollerLineraLayout在同一项目的不同包中。

我尝试在ScrollerLinearLayout中使用getParent()函数,但它不起作用。

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

有人能帮我吗?谢谢!


2
什么具体地方出了问题? - FD_
1
@FD_ 这是一个空指针异常,出现在 RelativeLayout r = (RelativeLayout) ((ViewGroup) this.getParent()).getParent(); - Salvatore Ucchino
ScrollerLinearLayout 中你在哪里调用这段代码? - FD_
在调用 init() 之前,您是否将其添加到 ViewGroup 中? - FD_
@FD_ 没有,你说的“添加到视图组”是什么意思?能给我解释一下吗? - Salvatore Ucchino
显示剩余3条评论
7个回答

95

getParent()方法返回的是ViewParent而非View。你需要对第一次调用getParent()进行强制类型转换:

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

根据 OP 的评论,这会导致 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();处的空指针异常。我不明白可能是什么问题... - Salvatore Ucchino
@Rajin,我添加了一节帮助你调试这个问题的文章部分。 - Phil
我检查了日志,发现ViewParent parent = this.getParent();返回null。我有这个public class ScrollerLinearLayout extends LinearLayout { RelativeLayout r; // some code public void init(){ // your code } },在我调用getParent()方法的地方是正确的吗? - Salvatore Ucchino

6
如果您正在尝试从Fragment中查找View,请尝试按照以下方式进行操作:
int w = ((EditText)getActivity().findViewById(R.id.editText1)).getLayoutParams().width;

ScrollerLineraLayout类继承自LinearLayout类,但是该类中的“getActivity()”方法未定义。 - Salvatore Ucchino

2
您可以使用以下代码获取任何视图: view.rootView.findViewById(R.id.*name_of_the_view*) 注意,在Java中,您可能需要执行类似于此的操作: this.getCurrentFocus().getRootView().findViewById(R.id.*name_of_the_view*) 我从@JFreeman的回答中学习了getCurrentFocus()函数。

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

0

父级作为视图

fun View.printParentSize() {
    log("parentWidth = ${(parent as View).sizeW}")
    log("parentHeight = ${(parent as View).sizeH}")
}

0

在这里查看我的答案

当您拥有复杂的视图或使用第三方库无法向视图添加ID时,使用布局检查器工具非常方便。


-2

这个也可以:

this.getCurrentFocus()

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接