需要使用已使用另一个 aar 的 android 库 (.aar)
Need to work with an android lib (.aar) that has used another aar
- 我创建了一个 android 库 (A.aar),它有一个 activity A-activity。 A-activity 使用它自己的 a.xml 和另一个来自不同库 (B.aar) 的 b.xml。 B 库中没有任何 activity,只有 b.xml 文件。
A-activity 能够正确使用 B 的 xml 并且我可以将两个 xml UI 一起充气作为同一个 activity 的一部分,如下图:
A.aar :
//A.java
public class A extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layoutMain = new LinearLayout(this);
layoutMain.setOrientation(LinearLayout.HORIZONTAL);
setContentView(layoutMain);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate(
R.layout.A, null);
RelativeLayout layoutRight = (RelativeLayout) inflate.inflate(
R.layout.B, null);
RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutMain.addView(layoutLeft, 100, 100);
layoutMain.addView(layoutRight, relParam);
}
}
//A.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/left"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/view1"
android:background="@color/white"
android:layout_width="fill_parent"
android:layout_height="50px" android:text="Text1" />
<TextView android:id="@+id/view2"
android:background="@color/white"
android:layout_width="fill_parent"
android:layout_height="50px" android:layout_below="@id/view1"
android:text="Text2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:id="@+id/button1"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@id/view2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:id="@+id/button2"
android:layout_below="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
B.aar :
//B.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/right"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/right_view1"
android:background="@color/white"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Text3" />
<TextView android:id="@+id/right_view2"
android:background="@color/white"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/right_view1" android:text="Text4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"
android:id="@+id/button3"
android:layout_below="@+id/right_view2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4"
android:id="@+id/button4"
android:layout_below="@+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
现在,为了检查它是否正常工作,我为 A-activity 创建了一个 .apk,我可以看到 xml 都正确显示为:
我们可以看到,A-activity正确使用了xmlA和B
但是,如果我从另一个应用程序 C.apk 启动此 A-activity 使用 intent as :
Intent i = new Intent( C.this,A.class);
startActivity(i);
在这种情况下 A.xml 完全重叠 B.xml 组件,如下所示:
如果有人遇到过此类问题,请提出建议。
我发现的问题是将两个 xml 膨胀为两个 RelativeLayout。这导致了问题。
A.java 解决问题的方法如下:
setContentView(R.layout.main);
LayoutInflater inflater = LayoutInflater.from(this);
View x= inflater.inflate(R.layout.A, null, false);
View y= inflater.inflate(R.layout.B, null, false);
LinearLayout LL = (LinearLayout) findViewById(R.id.layout_left);
LinearLayout RL = (LinearLayout) findViewById(R.id.layout_right);
LL.addView(x);
RL.addView(y);
- 我创建了一个 android 库 (A.aar),它有一个 activity A-activity。 A-activity 使用它自己的 a.xml 和另一个来自不同库 (B.aar) 的 b.xml。 B 库中没有任何 activity,只有 b.xml 文件。
A-activity 能够正确使用 B 的 xml 并且我可以将两个 xml UI 一起充气作为同一个 activity 的一部分,如下图:
A.aar :
//A.java
public class A extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layoutMain = new LinearLayout(this);
layoutMain.setOrientation(LinearLayout.HORIZONTAL);
setContentView(layoutMain);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layoutLeft = (RelativeLayout) inflate.inflate(
R.layout.A, null);
RelativeLayout layoutRight = (RelativeLayout) inflate.inflate(
R.layout.B, null);
RelativeLayout.LayoutParams relParam = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutMain.addView(layoutLeft, 100, 100);
layoutMain.addView(layoutRight, relParam);
}
}
//A.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/left"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/view1"
android:background="@color/white"
android:layout_width="fill_parent"
android:layout_height="50px" android:text="Text1" />
<TextView android:id="@+id/view2"
android:background="@color/white"
android:layout_width="fill_parent"
android:layout_height="50px" android:layout_below="@id/view1"
android:text="Text2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:id="@+id/button1"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@id/view2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:id="@+id/button2"
android:layout_below="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
B.aar :
//B.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/right"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/right_view1"
android:background="@color/white"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="Text3" />
<TextView android:id="@+id/right_view2"
android:background="@color/white"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/right_view1" android:text="Text4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"
android:id="@+id/button3"
android:layout_below="@+id/right_view2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4"
android:id="@+id/button4"
android:layout_below="@+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
现在,为了检查它是否正常工作,我为 A-activity 创建了一个 .apk,我可以看到 xml 都正确显示为:
我们可以看到,A-activity正确使用了xmlA和B
但是,如果我从另一个应用程序 C.apk 启动此 A-activity 使用 intent as :
Intent i = new Intent( C.this,A.class); startActivity(i);
在这种情况下 A.xml 完全重叠 B.xml 组件,如下所示:
如果有人遇到过此类问题,请提出建议。
我发现的问题是将两个 xml 膨胀为两个 RelativeLayout。这导致了问题。
A.java 解决问题的方法如下:
setContentView(R.layout.main);
LayoutInflater inflater = LayoutInflater.from(this);
View x= inflater.inflate(R.layout.A, null, false);
View y= inflater.inflate(R.layout.B, null, false);
LinearLayout LL = (LinearLayout) findViewById(R.id.layout_left);
LinearLayout RL = (LinearLayout) findViewById(R.id.layout_right);
LL.addView(x);
RL.addView(y);