我是Java Android开发的初学者。我在res / layout /中有两个XML文件。命名为 activity_fast_tip.xml 和 activity_setting.xml 我无法将这两个组件连接在一起。在activity_setting.xml
@Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView(R.layout.activity_fast_tip);
tipPctTextView=(TextView)this.findViewById(R.id.tipPctTextView);
tipAmountTextView=(TextView)this.findViewById(R.id.tipAmtTextView);
totalAmountTextView=(TextView)this.findViewById(R.id.totalAmtTextView);
calcTipAmountButton=(Button)this.findViewById(R.id.calcTipButton);
billAmountTextView=(EditText)this.findViewById(R.id.billAmtEditText);
calcTipAmountButton.setOnClickListener(new onClickListener()
{
@Override
public void onClick(View v) {
calculateTip();
}
});
}
并在activity_fast_tip
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".FastTipActivity" >
<EditText
android:id="@+id/billAmtEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:ems="10"
android:gravity="right|center_vertical"
android:hint="@string/billAmount"
android:inputType="number|numberSigned|numberDecimal" >
<requestFocus />
</EditText>
<Button
android:id="@+id/calcTipButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/billAmtEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="@string/calculateTip" />
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/billAmtEditText"
android:layout_below="@id/calcTipButton"
android:layout_marginTop="18dp"
android:text="@string/tipPercentage"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/TextView01"
android:layout_below="@+id/TextView01"
android:layout_marginLeft="0dp"
android:layout_marginTop="18dp"
android:text="@string/tipAmount"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="18dp"
android:text="@string/totalAmount"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FF0000" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/billAmtEditText"
android:layout_marginBottom="35dp"
android:text="@string/button" />
答案 0 :(得分:0)
使用布局inflater
http://developer.android.com/reference/android/view/LayoutInflater.html
或包含
<include
android:layout_width="fill_parent"
android:layout_height="fill_parent"
layout="@layout/activity_setting"
/>
答案 1 :(得分:0)
include
tag这样的布局用于another layout
titlebar.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:background="@color/titlebar_bg">
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/gafricalogo" />
</FrameLayout>
some other layout
我们在其中包含titlebar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="@color/app_bg"
android:gravity="center_horizontal">
<include layout="@layout/titlebar"/>
<TextView android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:text="@string/hello"
android:padding="10dp" />
...
</LinearLayout>
http://developer.android.com/training/improving-layouts/reusing-layouts.html
2. 你可以使用LayoutInflater
code snippet
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View someView = inflater.inflate(R.layout.ur_layout,null); //2nd parameter is for viewgroup
which_layout_u_want_to_add.addView(someView);
http://developer.android.com/reference/android/view/LayoutInflater.html