Android的添加按钮,碎片没有ID [英] Android add button to Fragment without id

查看:254
本文介绍了Android的添加按钮,碎片没有ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个片段的左下边角显示一个按钮,但我不知道该片段的主要布局的编号,因为我在一个没有布局的抽象类。

I want to display a button in the left down corner of a fragment but I don't know the Id of the fragment's main layout as I am in an abstract class that has no layout.

@EFragment(R.layout.exercise_test)
public class Exercise1 extends Exercice

public abstract class Exercice extends Fragment{

   public void showValidateButton()
   {
       Button validateButton = new Button(this.getActivity().getBaseContext());
       validateButton.setText("OK");
       // Here I want to add the button to Exercise1 
   }
}

有没有办法实现这一目标?

Is there any way to achieve this ?

感谢您

蒂埃里。

推荐答案

您可以使用该片段的的 getView() 方法获取含有该片段的所有布局的根视图。只要在这是一个的ViewGroup 您可以添加使用addView()方法的按钮。

You can use the Fragment's getView() method to get the root view containing all the Layout of the Fragment. As long at this is a ViewGroup you can add your Button using the addView() method.

不过的地方,如果正确的话,你就必须对使用的布局,如一些知识RelativeLayout的,等的LinearLayout

However to place if correctly, you'd have some knowledge about the Layout used, e.g. RelativeLayout, LinearLayout etc.

一个更铁杆的解决办法是创建自己的RelativeLayout,加片段的getView()和您自己的按钮的结果,并设置您的RelativeLayout为片段的新根视图。

A more hardcore solution would be to create your own RelativeLayout, add the result of Fragment's getView() and your own Button and set your RelativeLayout as the new Root View of the Fragment.

这篇关于Android的添加按钮,碎片没有ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆