android 线性布局位置,android – 如何在线性布局中更改视图的位置.

博客围绕Android线性布局展开,提出能否改变已填充子项中某一个孩子位置的问题,旨在交换子项位置。还给出了相关代码示例,包括创建线性布局、添加视图、设置按钮点击和长按事件等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

所以我有一个已经填充了子项的线性布局.有没有办法改变其中一个孩子的位置?

如果有任何帮助的话,我正试图交换他们之间的观点.

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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值