【问题标题】:Reffering to an ID in different XML file引用不同 XML 文件中的 ID
【发布时间】:2016-03-04 20:27:31
【问题描述】:

我有我的 MapsActivity,它有一个 onCreateMethod() 作为

setContentView(R.layout.activity_maps);

我调用了一个对话框,可以将当前活动覆盖一段时间

Dialog dialog = new Dialog(MapsActivity.this);
dialog.setContentView(R.layout.dialog);
dialog.setCancelable(false);
dialog.show();

指的是dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:gravity="center">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Enter Destination Name"
            android:id="@+id/textView8" />
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:gravity="center">
        <fragment
            android:id="@+id/place_autocomplete_fragment"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" />
    </LinearLayout>
</LinearLayout>

我希望我的程序从 MapsActivity 引用到 R.id.place_autocomplete_fragmentdialog.xml

我尝试使用R.id.place_autocomplete_fragment,但没有成功。

那么我该怎么做呢?帮助

【问题讨论】:

    标签: android xml


    【解决方案1】:

    尽量不要将您的片段直接写入 xml。而是为您的片段分配一个持有者视图,然后使用片段管理器以编程方式将该片段添加到该空间中。

    【讨论】:

    • 什么意思?我对 android 不是很好,只是平均水平:P
    • 我假设您有一个包含 Google 自动填充位置的片段。我想你可能会通过查看这个 stackoverflow 来更好地了解如何设置它:stackoverflow.com/questions/30089123/… 你可以看到他们正在用他们想要使用 FragmentManager(和 Fragment Transaction)的 Fragment 替换视图。
    • 不,这不是问题,我想要的是与 .java 不同的布局被引用。 :P
    【解决方案2】:

    你可以这样做

    dialog.findViewById(R.id.place_autocomplete_fragment)
    

    您将在 MapsActivity 本身中获得该实例。 你的问题对我来说不是很清楚,所以希望这个答案有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多