[英]Adding multiple framelayout in a linearlayout programmatically
I need to overlap an image view with textview. 我需要将图像视图与textview重叠。 And this combined view will be repeated 100 times in a LinearLayout.
并且此组合视图将在LinearLayout中重复100次。 I was thinking of using FrameLayout in LinearLayout and Repeating the FrameLayout in LinearLayout 100 times when FrameLayout holds the imageview and textview overlapped.
我当时想在LinearLayout中使用FrameLayout,并在FrameLayout保持imageview和textview重叠时,在LinearLayout中重复FrameLayout 100次。 Need to do this programatically not from xml file.
需要以编程方式而不是从xml文件执行此操作。
I added the image and textview to framelayout first then tried to add the framelayout to linearlayout. 我先将图像和textview添加到framelayout中,然后尝试将framelayout添加到linearlayout中。 But it says : the specified child has already a parent.. so not working.
但它说:指定的孩子已经有一个父母..所以不工作。 Can you please show me in code?
你能给我看看代码吗? Thanks for your help.
谢谢你的帮助。
it is going to be like this, but need to be done programmaticaly 将会是这样,但是需要以编程方式完成
---linear layout--------------
------------------------------
|frame layout----------------|
||txt view on top of img view|
------------------------------
frame layout will be repeated|
---/end of linear layout------
Also here is the separated code: 这也是分开的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
LinearLayout dynamicview = (LinearLayout) findViewById(R.id.main_layout);
FrameLayout barFrameLayout = new FrameLayout(this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT,
Gravity.CENTER);
barFrameLayout.setLayoutParams(params);
LinearLayout.LayoutParams slparams1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
for (int i = 65; i <= 75; i++) {
TextView catTV = new TextView(this);
catTV.setLayoutParams(slparams1);
catTV.setText("===" + Character.toString((char) i) + "===");
catTV.setTextSize(32);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.ic_launcher);
iv.setLayoutParams(slparams1);
barFrameLayout.addView(catTV);
barFrameLayout.addView(iv);
dynamicview.addView(barFrameLayout);
}
}
Here is the code to demonstrate what you are trying to achieve. 这是演示您要实现的代码。 I have used
RelativeLayout
, which is very flexible, you can position the elements easily relative to others.( if you need to change to FrameLayout
you can change ). 我使用了
RelativeLayout
,它非常灵活,您可以相对于其他元素轻松地FrameLayout
元素。(如果需要更改为FrameLayout
,可以更改)。
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ExampleLayout extends LinearLayout{
public ExampleLayout(Context context,AttributeSet attrs){
super(context,attrs);
for(int i =0; i< 100; i++){
RelativeLayout childLayout = new RelativeLayout(context);
ImageView img = new ImageView(context);
TextView text = new TextView(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
childLayout.addView(img, params);
params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
childLayout.addView(text, params);
LinearLayout.LayoutParams parentParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.addView(childLayout,parentParams);
}
}
}
You can then use the ExampleLayout class to add it to any of the layout.xml file. 然后,您可以使用ExampleLayout类将其添加到任何layout.xml文件中。
FrameLayout is designed to block out an area on the screen to display a single item
FrameLayout旨在遮挡屏幕上的某个区域以显示单个项目
(source: http://developer.android.com/reference/android/widget/FrameLayout.html ). (来源: http : //developer.android.com/reference/android/widget/FrameLayout.html )。
Anyway, you have to create new FrameLayouts not use the same. 无论如何,您必须创建新的不使用相同的FrameLayouts。
When you're doing: 在执行操作时:
barFrameLayout.addView(catTV);
barFrameLayout.addView(iv);
dynamicview.addView(barFrameLayout);
you're always adding these new objects (catTV and iv) to the same instance of a FrameLayout (barFrameLayout). 您总是将这些新对象(catTV和iv)添加到FrameLayout的同一实例(barFrameLayout)。
I don't think that's what you wanted to do. 我认为那不是您想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.