怎样对layout进行移动 并且 addView

yitiaochongzi 2012-06-14 02:50:56
Layout 调用 layoutlayout(int l, int t, int r, int b) 方法后 变换了位置
但是在调用了 addView 方法后 位置又变了回去
AndroidTestDemoActivity.java
package com.view.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;

public class AndroidTestDemoActivity extends Activity {
private Button bt, bt2;
private FrameLayout layout;
private FrameLayout container;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
layout.layout(80, 0, 80 + layout.getWidth(), layout.getHeight());
layout.postInvalidate();
}
});
bt2 = (Button) findViewById(R.id.button2);
bt2.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
layout.addView(LayoutInflater.from(AndroidTestDemoActivity.this).inflate(R.layout.view, null));
}
});
layout = (FrameLayout) findViewById(R.id.layout);
container = (FrameLayout) findViewById(R.id.container);
}
}


main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Layout" />

<Button android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="AddView" />


<FrameLayout android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff00ff00" >

<FrameLayout android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff00ff00" />

</FrameLayout>

</LinearLayout>


view.xml
[code=XML]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@android:color/white" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello" />


</LinearLayout>/code]

怎样操作 才能使 layout变换位置后 addView 位置 不会改变
...全文
392 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ericllzh 2012-06-29
  • 打赏
  • 举报
回复
用viewgroup,基础的类来写.
嗨皮 2012-06-28
  • 打赏
  • 举报
回复
你继承一个ViewGroup自己写一个类,想怎么移就怎么移啊
gongxujun 2012-06-28
  • 打赏
  • 举报
回复
这个要做的话还得再建个类
dinko321 2012-06-28
  • 打赏
  • 举报
回复
不用layout方法,改layoutparam再刷新界面说不定可行
yitiaochongzi 2012-06-15
  • 打赏
  • 举报
回复
怎么 没人回答阿

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧