[英]Load fragment with layout from another APK
我有兩個 APK。 第一個 APK 是加載第二個 APK 的主 APK。
MainActivity.java(第一個 APK):對象mainFragment
被 DexClassLoader 提前加載
setContentView(R.layout.activity_main);
LinearLayout fragContainer = findViewById(R.id.main_fragment);
LinearLayout ll = new LinearLayout(context);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setId(View.generateViewId());
getSupportFragmentManager().beginTransaction().add(ll.getId(), mainFragment, "mainFragment").commit();
fragContainer.addView(ll);
activity_main.xml(第一個 APK):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/main_fragment"
android:orientation="vertical"
/>
</LinearLayout>
MainFragment.java(第二個 APK):
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, viewGroup, false);
view.findViewById(R.id.emailSignInButton).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
return view;
}
fragment_main.xml(第二個 APK):
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="@string/sign_in"
android:layout_width="88dp"
android:layout_height="wrap_content"
android:id="@+id/emailSignInButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="32dp"
android:layout_marginEnd="32dp" android:layout_marginTop="8dp"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
我不明白,為什么要排隊
View view = inflater.inflate(R.layout.fragment_main, viewGroup, false);
view
始終為 NULL。 我是否需要像mainFragment
一樣將R.layout.fragment_main
加載到內存中?
@CommonsWare 已經回答了您:您正在加載類,而不是資源。
在 Android 的包中,您有類(如 R)和資源(預處理的 XML 文件,如布局)。 ClassLoader 能夠讀取和加載第一個類,因此,加載另一個 APK R 類是可能的。 但是 R 的索引只是指向資源文件,它們不包含所述資源的實際值。 因此,您旁加載的 R 類不提供到有效資源的路由。 如果 R 可以做到這一點,那么 APK/AAR/APKLib 格式將不存在; 常規的 JAR 文件就足夠了。 但它們不會,因為 android 資源文件與類文件不同,因此,需要某種“鏈接”才能使它們對您的類可見:R 類。
如果你想讓你當前的代碼工作,你需要用一個編碼的視圖組實例替換 fragment_main.xml。 創建一個類/方法,提供一個包含按鈕的約束布局實例(例如 MyViewFactory.createConstraintLayoutWithButton),通過代碼設置屬性,並替換
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, viewGroup, false);
view.findViewById(R.id.emailSignInButton).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
return view;
}
像
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
View view = MyViewFactory.createConstraintLayoutWithButton(getContext)
view.findViewById(R.id.emailSignInButton).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
return view;
}
只要你不使用資源,你會沒事的。 也就是說,這種方法非常脆弱,所以也許最好在您的 APK 之間創建意圖過濾器,以使 APK 2 為 APK 1 工作。
編輯:您可以使用 R 常量作為代碼生成視圖的 id,只要您先手動設置它們。 例如:
public static ConstraintLayout createConstraintLayoutWithButton(Context context){
Constraintlayout mylayout = new ConstraintLayout(context);
//set up of the layout
Button myButton = new Button(context);
//set up of the button
button.setId(R.id.emailSignInButton);
myLayout.addView(button);
return mylayout;
}
然后你可以找到帶有 id 的按鈕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.