如何更改视图的线性布局的位置? [英] How do I change the position of a view in a Linear Layout.?
本文介绍了如何更改视图的线性布局的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有一个线性布局已经填充了孩子。有没有办法来改变其在一个孩子所在的位置?
我想交换到它们之间的意见,如果这是任何帮助。
最后的LinearLayout parrent =(的LinearLayout)findViewById(R.id.llWidgetScreen);
最终的LinearLayout Delailah =新的LinearLayout(本);
Delailah.setLayoutParams(新android.widget.LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
AppWidgetHostView wedgy = attachWidget(mAppWidgetHost.createView(这一点,appWidgetId,appWidgetInfo));
Delailah.addView(wedgy);
最终按钮BTN =新按钮(本);
btn.setLayoutParams(新android.widget.LinearLayout.LayoutParams((INT)(20 *规模+ 0.5F),android.view.ViewGroup.LayoutParams.FILL_PARENT,0F));
btn.setOnLongClickListener(新OnLongClickListener(){ @覆盖
公共布尔onLongClick(视图v){
parrent.removeView(Delailah);
返回false;
}
});
btn.setBackgroundColor(mainColor);
btn.setText(parrent.getChildCount()+ 1+);
btn.setTextColor(文字颜色);
btn.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
如果(ReadyForDrag == 0)
{
btn.setBackgroundColor(actiColor);
ReadyForDrag ++;
DragPosition1 =的Integer.parseInt(btn.getText()的toString());
}
否则如果(ReadyForDrag == 1)
{
btn.setBackgroundColor(actiColor);
ReadyForDrag = 0;
V1的LinearLayout =(的LinearLayout)parrent.getChildAt(DragPosition1);
V2的LinearLayout =(的LinearLayout)parrent.getChildAt(的Integer.parseInt(btn.getText()的toString()));
//移动视图2比1位
//移动视点1到2位
}
}
});
Delailah.addView(BTN);
parrent.addView(Delailah);
解决方案
您可以使用 ViewGroup.removeView(视图)
和<一个href=\"http://developer.android.com/reference/android/view/ViewGroup.html#addView%28android.view.View,%20int,%20android.view.ViewGroup.LayoutParams%29\"><$c$c>ViewGroup.addView(View孩子,INT指数,ViewGroup.LayoutParams PARAMS) 这一点。
So I have a Linear Layout already populated with children. Is there a way to change the position at which one of the children is located?
I'm trying to swap to views between them if that's of any help.
final LinearLayout parrent = (LinearLayout)findViewById(R.id.llWidgetScreen);
final LinearLayout Delailah = new LinearLayout(this);
Delailah.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
AppWidgetHostView wedgy = attachWidget(mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo));
Delailah.addView(wedgy);
final Button btn = new Button(this);
btn.setLayoutParams(new android.widget.LinearLayout.LayoutParams((int)(20*scale +0.5f), android.view.ViewGroup.LayoutParams.FILL_PARENT, 0f));
btn.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
parrent.removeView(Delailah);
return false;
}
});
btn.setBackgroundColor(mainColor);
btn.setText(parrent.getChildCount()+1+"");
btn.setTextColor(textColor);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(ReadyForDrag==0)
{
btn.setBackgroundColor(actiColor);
ReadyForDrag++;
DragPosition1=Integer.parseInt(btn.getText().toString());
}
else if(ReadyForDrag==1)
{
btn.setBackgroundColor(actiColor);
ReadyForDrag=0;
LinearLayout v1 = (LinearLayout)parrent.getChildAt(DragPosition1);
LinearLayout v2 = (LinearLayout)parrent.getChildAt(Integer.parseInt(btn.getText().toString()));
//move view 2 to position 1
//move view 1 to position 2
}
}
});
Delailah.addView(btn);
parrent.addView(Delailah);
解决方案
You can use ViewGroup.removeView(View)
and ViewGroup.addView(View child, int index, ViewGroup.LayoutParams params)
for this.
这篇关于如何更改视图的线性布局的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文