我正在尝试在运行时创建视图,我想让主布局视图为空白,然后在运行时添加一个虚线框视图(组视图),然后在该视图中添加其他视图(项目视图)。我有这个工作,但我目前正在使用两个组视图进行测试,但是当它们被添加时,它们会相继添加,并且虚线视图之间没有间距。我真的不明白为什么会这样,因为我已经尝试了每个边距和填充的组合来尝试这样做,但没有任何作用。请有人帮我这个。提前谢谢。
以下是我的组布局视图代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/group_box"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:id="@+id/pvGroupContainerLayout" >
<TextView
android:id="@+id/groupBoxTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="8dp"
android:paddingTop="4dp"
android:paddingBottom="@dimen/activity_vertical_margin" />
</LinearLayout>
以下是添加视图的代码:
public void createView()
{
deviceInfoContainer = new DeviceInfoContainer(activity);
TextView deviceInfoTextView1 = deviceInfoContainer.getDeviceInfoTextView1();
TextView deviceInfoTextView2 = deviceInfoContainer.getDeviceInfoTextView2();
TextView deviceInfoTextView3 = deviceInfoContainer.getDeviceInfoTextView3();
View deviceInfoView = deviceInfoContainer.getDeviceInfoView();
deviceInfoTextView1.setText(deviceInfo.getDeviceTypeName());
deviceInfoTextView2.setText(String.valueOf(deviceInfo.getDeviceTypeCode()));
deviceInfoTextView3.setText(String.valueOf(deviceInfo.getSensorTypeObject()));
pvContainerLayout.addView(deviceInfoView);
System.out.println("Page test: " + page.getAttributeName());
ArrayList<Group> groups = page.returnGroups();
System.out.println("Groups test: " + groups.size());
for(Group g : groups)
{
groupContainer = new PVGroupContainer(activity);
groupContainerList.add(groupContainer);
TextView groupTextView = groupContainer.getGroupTextView();
View groupView = groupContainer.getGroupView();
groupTextView.setText(g.getAttributeName());
pvContainerLayout.addView(groupView);
for(Item i : g.returnItems())
{
DataObject dataObject = i.getDataObject();
if(dataObject.getClass().equals(StringDataObject.class))
{
StringDataObject stringDataObject = (StringDataObject) dataObject;
itemContainer = new PVItemContainer(activity); // Declares new object in the array list
itemContainerList.add(itemContainer);
TextView progressBarTextView1 = itemContainer.getProgressBarTextView1();
TextView progressBarTextView2 = itemContainer.getProgressBarTextView2();
ProgressBar progressBarProgressBar1 = itemContainer.getProgressBarProgressBar1();
View progressBarView = itemContainer.getProgressBarView();
ImageButton progressBarImageButton1 = itemContainer.getProgressBarImageButton1();
progressBarTextView1.setText(i.getAttributeName());
progressBarTextView2.setText(stringDataObject.getValue());
progressBarProgressBar1.setProgress(0); //TODO: String will be "" which is invalid. May need to change the view.
((ViewGroup) groupView).addView(progressBarView); // Adds the view to the current layout
progressBarView.setOnLongClickListener(new OnLongClickListener()
{
@Override
public boolean onLongClick(View v) {
Vibrator vibrator = (Vibrator)activity.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1500);
popupFirmware.createDialog();
return true;
}
});
progressBarImageButton1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
Fragment fragment = new Fragment_3();
FragmentTransaction transaction = ((FragmentActivity) activity).getSupportFragmentManager().beginTransaction();
transaction.replace(R.layout.fragment_diag, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
}
}