【问题标题】:What is a rootView?什么是根视图?
【发布时间】:2012-08-28 20:18:07
【问题描述】:

您好,我是 Android 新手,我使用默认模板 Master detail flow 打开了一个项目,但我不知道什么是 rootView。我在 developer.android.com 上搜索过,但我不是很明白...

所以,如果有人能清楚地解释我这件事是做什么的,那将对我有很大帮助。

谢谢!

编辑:有代码!

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments().containsKey(ARG_ITEM_ID)) {
        mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);
    if (mItem != null) {
        mLinearLayout = (LinearLayout)findViewById(R.id.layout); 
    }
    return rootView;
}

【问题讨论】:

    标签: android hierarchy


    【解决方案1】:

    RootView 是放置所有其他视图的视图。它就像树结构中的根节点,是所有子节点的父节点。

    例如,您的布局中有多个按钮,它们放置在 LinearLayout 中。然后 LinearLayout 被称为 RootView,因为它在结构中的位置最高,所有东西都必须放在里面。

    希望这能消除您的疑虑。

    【讨论】:

    • 感谢您的回答,它对我有很大帮助,但在上述模板中,rootview 是一个 textview,我无法更改布局 xml 文件......如果我在 rootview 中放置一个 relativeLayout 可以我更改布局文件?
    • 是的,我的朋友,你可以!问题是 TextViews 实际上并不是设计为其他 vie 的父视图,因为它们不能在其中容纳任何东西。但是,如果您有一个布局作为您的 RootView,那么您可以在其中添加任意数量的视图并按照您想要的方式更改您的布局。
    • Relative、Linear、Table、Frame.. 有很多不同类型的布局可以用作 rootView,然后相应地更改布局。
    • 最后一个问题是什么代码来做因为这个代码'((LinearLayout)rootView.findViewById(R.id.layout));'不工作
    • 选择mLLinearLayout = (LinearLayout)findViewById(R.id.layout);
    【解决方案2】:

    这是一个视图,通常是托管所有其他视图的 ViewGroup。

    This 是 Android 上布局如何工作的一个很好的起点,其中有一个示例,其中 LinearLayout 用作根元素。

    【讨论】:

    • 谢谢你的回答,我会仔细看看!
    【解决方案3】:

    显示的元素按树形层次结构组织。例如,如果您将 Button 放在 Linear Layout 容器中,则 LinearLayout 是所述 Button 的父级。没有“高于”的最高父级是“根”。同样适用于文件夹和文件——你得到父文件夹,还有根文件夹——在层次结构的最顶端

    【讨论】:

    • 感谢您的回答,您帮了我很多!
    猜你喜欢
    • 2018-05-26
    • 1970-01-01
    • 2017-06-05
    • 2011-10-28
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多