这是我的布局结果
我的问题是我不希望“新资产”(单击新资产按钮时女巫显示)和“旧资产”之间的距离太长,“旧资产”必须在按钮下方并且“单击按钮时,新资产”应放置在旧资产之上。
这是我的 xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroller"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<LinearLayout
android:id="@+id/contain"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.01"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="@+id/a"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Build a list of significant assets for the organization"
android:textSize="14sp"
android:textStyle="bold" >
</TextView>
<Button
android:id="@+id/addasset"
style="@style/btnStyleBlue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text=" + add new asset" />
<LinearLayout
android:id="@+id/newa"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="0.01"
android:orientation="vertical"
android:padding="10dp" >
</LinearLayout>
<LinearLayout
android:id="@+id/old"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="0.01"
android:layout_gravity="top"
android:orientation="vertical"
android:padding="10dp" >
</LinearLayout>
</LinearLayout>
</ScrollView>
和我的java代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.assets, container, false);
final LinearLayout newa = (LinearLayout) rootView
.findViewById(R.id.newa);
final LinearLayout old = (LinearLayout) rootView
.findViewById(R.id.old);
Button add_asset = (Button) rootView.findViewById(R.id.addasset);
add_asset.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
TextView tv1 = new TextView(v.getContext());
tv1.setText("New asset ");
tv1.setTextSize(14);
tv1.setTypeface(null, Typeface.BOLD);
tv1.setPadding(0, 15, 0, 10);
newa.addView(tv1);
}
});
TextView tv = new TextView(rootView.getContext());
tv.setText("old asset ");
tv.setTextSize(14);
tv.setTypeface(null, Typeface.BOLD);
tv.setPadding(0, 15, 0, 10);
old.addView(tv);
return rootView;
}