2017-01-09 188 views
1
  1. 我创建了一个具有一个活动A-activity的Android库(A.aar)。 A-activity使用自己的a.xml和另一个lib(B.aar)中的另一个b.xml。 B lib没有任何活动,只有b.xml文件。

A-活性是能够正确地使用B的XML和我可以一起膨胀两个XML UI作为相同的活性的部分,如下所示:需要使用已使用另一个aar的android库(.aar)

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-活动.apk文件,我可以同时看到XML是显示正确LY为:

A-activity using xml A and B

我们可以看到,使用XML A和B正确A-活动。

  • 但是如果我使用意图作为从不同的应用程序C.apk劳克此A-活动:

    Intent i = new Intent(C.this,A.class); 
    startActivity(i); 
    
  • 在这种情况下是A.XML完全overlaping B.XML组件,如下图所示:

    enter image description here

    请建议,如果任何人遇到这种问题。

    回答

    0

    我发现的问题是两个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);