当前位置: 首页 > 知识库问答 >
问题:

如何在java中添加到线性布局(无XML)

柳修为
2023-03-14

我想在线性布局(垂直)中添加一行文本

我想在java的函数中添加它们

如何做到这一点?

共有3个答案

苏培
2023-03-14

使用addView:

LinearLayout layoutCard  = new LinearLayout(getApplicationContext());
TextView text = new TextView(getApplicationContext());
text.setText("hello");
layoutCard.addView(text);
公羊向荣
2023-03-14

像这样做:

LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);

//add layout params
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);

//add TextView inside the vertical LinearLayout
TextView textView = new TextView(this);
textView.setTextSize(16); //set the text size here
textView.setTextColor(Color.BLACK); // set the text color here
textView.setText("Texts"); // set the text here
textView.setTypeface(Typeface.BOLD); // set the text style here
linearLayout.addView(textView, p); // add the TextView to the LinearLayout

您可以根据需要在线性布局中添加任何视图。

荀豪
2023-03-14

要在Java中动态添加,请编写如下代码:LinearLayout myLayout=newlinearlayout(MainActivity.this);

 类似资料:
  • 我尝试过使用阴影xmls进行线性布局,但似乎没有按照我想要的方式进行。我使用的代码是: 请建议我如何实现这一点。

  • 我是Android新手,我正在尝试如何做到这一点:-我有一个带有ScrollView的活动,其中有一个LinearLayout(R.id.my_layout)-我需要以编程方式添加TextView,所以我正在做: 我通过seContentView加载主XML布局,我在ScrollView中将我的LinearLayout称为“mLayout”等等。我从文件中加载一个名称列表,并使用一个名为填充列表(

  • 我正在尝试添加具有线性布局的背景图像作为根布局[Look hierarchy diagram for reference.]我曾尝试使用相对布局作为根布局,但它与嵌套的线性布局重叠。当线性布局作为根布局时,它只显示在底部。 XML代码 查看层次结构图

  • 问题内容: 我想在我的 底栏布局的中间添加Facebook Messenger,例如超大按钮,但困惑如何添加。 我正在使用ahbottomnavigation库来制作我的底栏。 问题答案: 我只是试图使其简单而不是专业类型。看这里: BottomSheetLayout文件- : ActivityLayout - Output: 根据您的想法改变价值和设计。 示例仅是一个演示,并不包含OP要求的确切

  • 按照这个答案,我能够在垂直RecolyerView的项目之间得到一个分界线。然而,我也想稍微缩进分隔线。

  • 我没有试图将任何视图居中,因为我的问题是,我有一条分界线应该从中间向下。为了做分割线,我做了一个LinearLayout并给它一个背景色。但是,两侧还有其他LinearLayouts。 你大概可以看出中间的LinearLayout就是构成线条的那个。我已经改变了很多android:layout_width值,也改变了很多android:gravity和android:layout_gravity值