具有TextView的FloatingActionButton动态

时间:2018-07-09 13:39:23

标签: android android-layout dynamically-generated

我想动态创建一个FloatingActionButton和textview,我想在xml根目录布局中添加它,但是FloatingActionButton存在一个隐藏我的textview的问题?

RelativeLayout main_rel_layout = findViewById(R.id.main_rel_layout);
TextView textView = new TextView(this);
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
textView.setText("Hello ");
textView.setTextSize(100);
textView.setLayoutParams(params);
textView.setGravity(Gravity.CENTER);
FloatingActionButton floatingActionButton = new FloatingActionButton(this);
floatingActionButton.setCustomSize(400);
relativeLayout.addView(textView);
relativeLayout.addView(floatingActionButton);
main_rel_layout.addView(relativeLayout);

我的xml代码

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_rel_layout">

<!--
    I want to add textview over floating button 
-->


    </RelativeLayout>

问题出在这里

textview is not showing

2 个答案:

答案 0 :(得分:1)

添加此代码以在文本视图上方设置

textView.bringToFront()

答案 1 :(得分:1)

    RelativeLayout main_rel_layout = findViewById(R.id.main_rel_layout);
    TextView textView = new TextView(this);
    RelativeLayout relativeLayout = new RelativeLayout(this);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.CENTER_IN_PARENT);
    textView.setText("Hello ");
    textView.setTextSize(100);
    textView.setLayoutParams(params);
    textView.setGravity(Gravity.CENTER);

    final FrameLayout frameLayout = new FrameLayout(this);
    FloatingActionButton floatingActionButton = new FloatingActionButton(this);
    floatingActionButton.setCustomSize(400);
    frameLayout.addView(floatingActionButton);

    relativeLayout.addView(frameLayout);
    relativeLayout.addView(textView);
    main_rel_layout.addView(relativeLayout);