从布局中获取父视图

时间:2013-07-26 11:05:25

标签: android android-layout view parent-child

我有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位于相同项目的不同包中。 我尝试在getParent()内以这种方式使用ScrollerLinearLayout函数,但它不起作用。

RelativeLayout r = (RelativeLayout) this.getParent().getParent();
int w = r.findViewById(R.id.editText1).getLayoutParams().width;
有人帮帮我吗?谢谢!

7 个答案:

答案 0 :(得分:67)

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.

答案 1 :(得分:6)

如果您尝试从View中找到Fragment,请尝试按以下步骤操作:

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

答案 2 :(得分:0)

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

答案 3 :(得分:0)

这也有效:

this.getCurrentFocus()

它获得了视图,所以我可以使用它。

答案 4 :(得分:0)

检查我的答案here

当您有复杂的视图或正在使用无法向视图添加ID的第三方库时,使用 Layout Inspector 工具会非常方便

答案 5 :(得分:0)

您可以使用以下代码获取任何视图

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

编辑:这在Kotlin上有效。在Java中,您可能需要执行以下操作=

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

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

答案 6 :(得分:-1)

只需写下

this.getRootView();
相关问题