gpt4 book ai didi

Android Fragment 顶级布局无法以编程方式访问

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:17:19 25 4
gpt4 key购买 nike

我无法通过在其 xml 中为布局指定的 id 到达 fragment 的顶层 '',它原来是一个空对象。但是我可以访问其 subview 的父 View 。该父项不是布局本身,而是包含布局的 ''。当我以编程方式将子项添加到此 fragment 时,它们不会与已经存在的子项对齐,这表明它们实际上位于嵌套布局中。

这种行为真的让我很困惑。谁能解释发生了什么?

我像这样声明一个顶级的 fragment 化布局:

Sibling fragments and layout settings stripped for readability

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_view"
>
<fragment
android:layout_gravity="center_horizontal|center_vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="40dp"
android:layout_weight="0.3"
android:name="foo.bar.foobar.fragments.ShowWeightsFragment"
android:id="@+id/show_weights_fragment"
tools:layout="@layout/frag_show_weights"
/>
</LinearLayout>

layout/frag_show_weights定义如下:

<LinearLayout 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"
android:id="@+id/layout_container_weights"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Foo"
android:id="@+id/xmlTextChildren"
android:layout_gravity="center" />

</LinearLayout>

MainActivity.java 如下所示

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t = (TextView) findViewById(R.id.xmlTextChildren);
t.setText("Foo");

LinearLayout parentLayout = (LinearLayout) findViewById(R.id.xmlTextChildren).getParent();
LinearLayout layoutFromID = (LinearLayout) findViewById(R.id.layout_container_weights);

TextView v = new TextView(this);
v.setText("Bar");
if (layoutFromID == null) Log.d(TAG, "Layout from ID is null");
Log.d(TAG, "ID parent name: " + getResources().getResourceEntryName(parentLayout.getId()));
parentLayout.addView(v);
}

日志输出如下:

08-22 12:06:24.855: D/MainActivity(2346): Layout from ID is null

08-22 12:06:24.856: D/MainActivity(2346): ID parent name: show_weights_fragment

而且实际的模拟器输出显示两个文本不对齐,这让我假设它们不包含在同一个 <LinearLayout> 中。 The emulated output on Android 5.22 running on a Nexus 5

最佳答案

简答:使用 ID R.id.show_weights_fragment如果您在 <fragment> 中设置了一个 ID,您的 fragment 就可以访问它的 View .


The documentation for Fragments有一个处理 fragment ID 的语句:

Note: Each fragment requires a unique identifier that the system can use to restore the fragment if the activity is restarted (and which you can use to capture the fragment to perform transactions, such as remove it). There are three ways to provide an ID for a fragment:

  • Supply the android:id attribute with a unique ID.
  • Supply the android:tag attribute with a unique string.
  • If you provide neither of the previous two, the system uses the ID of the container view.

这激发了假设,如果 fragment 的 id 已设置,它只是以相反的方式工作。我找不到关于此的文档,但是 the sources for FragmentManagerImpl 表明,这个假设成立:

public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
if (!"fragment".equals(name)) {
return null;
}

[...]

int id = a.getResourceId(com.android.internal.R.styleable.Fragment_id, View.NO_ID);
String tag = a.getString(com.android.internal.R.styleable.Fragment_tag);

[...]

if (id != 0) {
fragment.mView.setId(id);
}
if (fragment.mView.getTag() == null) {
fragment.mView.setTag(tag);
}
return fragment.mView;
}

这也是您的日志显示的内容:parentLayout.getId() == R.id.show_weights_fragment .

关于Android Fragment 顶级布局无法以编程方式访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32154948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com