繁体   English   中英

如何通过onClick将视图添加到Fragment的LinearLayout中?

[英]How to add view into LinearLayout of Fragment by onClick?

我有一个片段,此布局是一个按钮。

单击按钮时,将textview添加到textview_container中。

如何?

myfragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00000000"
    android:orientation="vertical" >
<LinearLayout 
    android:id="@+id/textview_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#00000000"
    android:orientation="horizontal" >
</LinearLayout>
<Button
                android:id="@+id/postbutton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:background="@null"
                android:text="@string/edit"
                android:textColor="@color/black"
                android:textSize="14sp" />
</LinearLayout>

Myfragment.java

public class AlbumListDetailFragment extends Fragment implements OnClickListener {
    Button postButton
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.myfragment  , container, false);
    }
    public void onStart() {
        super.onStart();
        initViews();
    }
     private void initViews() {
          button = (Button) getView().findViewById(R.id.postbutton);
          button.setOnClickListner{new View.onClickListener(){
           public void onClick(View v) {
               //make and Textview  into textview_container
           }
          }
     }
}
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.textview_container);
TextView valueTV = new TextView(this);
linearLayout.addView(valueTV);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM