Android 将动态线性布局添加到当前视图中

Android 将动态线性布局添加到当前视图中,android,android-layout,android-linearlayout,android-scrollview,Android,Android Layout,Android Linearlayout,Android Scrollview,我正在尝试将线性布局添加到scrolview 这是我的密码 代码可以编译,但没有显示新的布局 这是原始布局(我想添加到其中) 代码编译良好,应用程序正在运行,但什么也没发生 其中一个.xml文件有问题吗 tnx您应该使用充气机, 更改: 与 LayoutInflater从相应的XML文件创建一个View对象 LayoutInflater li = LayoutInflater.from(context); LinearLayout ll = (LinearLayout) li.infla

我正在尝试将线性布局添加到scrolview 这是我的密码 代码可以编译,但没有显示新的布局

这是原始布局(我想添加到其中)

代码编译良好,应用程序正在运行,但什么也没发生 其中一个.xml文件有问题吗


tnx

您应该使用充气机, 更改:

LayoutInflater
从相应的XML文件创建一个
View
对象

  LayoutInflater li = LayoutInflater.from(context);
  LinearLayout ll = (LinearLayout) li.inflate(R.layout.translation_menu, this)

这是正确的方法。

使用
LayoutInflator

LayoutInflater
类用于将布局XML文件实例化为其相应的视图对象

换句话说,它接受一个XML文件作为输入,并从中构建视图对象

ScrollView sv = new ScrollView(this);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );   
LinearLayout ll = (LinearLayout) li.inflate(translation_menu, this);  
sv.addView(ll);

tnx!什么是变量上下文?您需要一个上下文对象来检索调用getSystemService。如果你在一个活动中,你可以直接调用getSystemServiceoh是的,我使用了getApplicationContect(),但它不工作,代码编译,应用程序正在运行,但我没有看到任何添加到布局中的内容。它可能与原始布局有关吗?
setContentView(R.layout.activity_view_history_image);
ScrollView sv = new ScrollView(this);
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View ll = inflater.inflate(R.layout.translation_menu, null);
sv.addView(ll);
LinearLayout ll = new LinearLayout(R.layout.translation_menu);
LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View ll = inflater.inflate(R.layout.translation_menu, null);
sv.addView(ll);
  LayoutInflater li = LayoutInflater.from(context);
  LinearLayout ll = (LinearLayout) li.inflate(R.layout.translation_menu, this)
ScrollView sv = new ScrollView(this);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );   
LinearLayout ll = (LinearLayout) li.inflate(translation_menu, this);  
sv.addView(ll);