如何在android中的片段中动态添加元素

时间:2016-07-15 20:05:00

标签: android android-fragments

我有片段,我想在点击浮动操作按钮时动态添加元素(textview,按钮)。

代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.xyz, container, false);

    FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            // new elements on click



            // new elements on click

        }
    });
    return view;
}

2 个答案:

答案 0 :(得分:3)

尝试片段

的xml代码
self.firstSquare = childNodeWithName("square") as! SKSpriteNode

let firstSquareCGPath:CGPath=CGPath(rect: CGRect(origin: self.firstSquare.position, size: self.firstSquare.size), transform: nil)

let shape:SKShapeNode=SKShapeNode(path: firstSquareCGPath, centered: true) //important
shape.zRotation = self.firstSquare.zRotation
shape.position = self.firstSquare.position
shape.fillColor = SKColor.blue()
addChild(shape)

Fragment的Java代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="10dp"
        app:srcCompat="@android:drawable/ic_dialog_email"/>
</FrameLayout>

答案 1 :(得分:0)

你可以这样做:

array:2 [▼
  "status" => "nok"
  "message" => "This slug is on a list of reserved names"
]

}

我没有测试过,但它应该完成这项工作