关于将两个LinearLayout添加到RelativeLayout里代码的添加方式和xml两种形式

前端之家收集整理的这篇文章主要介绍了关于将两个LinearLayout添加到RelativeLayout里代码的添加方式和xml两种形式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

例如:CMAdvertisement.java
//第七传媒 添加广告
public static void addMediaAdvertisement() {
GEInstance geInstance = new GEInstance();
geInstance.initialize(ApplicationDemo.activity,null,null);// 每次启动需要先初始化数据
geInstance.setOnInfoListener(ApplicationDemo.applicationDemo);// 继承GEInfoListener接口并监听自定义数据接口
geInstance.setDefalutscore(200);// 设置软件默认积分(默认为0)
geInstance.setscoreParam(10);// 设置金蛋和虚拟币的转换率(例如某软件金蛋价值为10,则此时它的虚拟价值为10*10=100)
geInstance.setNotificationIcon(R.drawable.icon);// 设置推送栏图标
geInstance.setSocreUnit("单位");// 设置虚拟货币的单位(默认为金币)
// 布局外框
ApplicationDemo.rlContainer = new RelativeLayout(ApplicationDemo.activity);//相对布局
ApplicationDemo.rlContainer.setGravity(Gravity.CENTER_HORIZONTAL);//居中

// 迷你广告
LinearLayout miniLinearLayout = new LinearLayout(ApplicationDemo.activity);
miniLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
//miniLinearLayout.setOrientation(LinearLayout.VERTICAL);
geInstance.loadMiniAd(10,GEMini.MINIFADE,miniLinearLayout);// 加载迷你广告
geInstance.setMiniBackColor(0xffff0000);// 设置迷你广告背景颜色
geInstance.setMiniTextColor(0xff00ff00);// 设置迷你广告文字颜色
//设置位置
RelativeLayout.LayoutParams rpMini = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
rpMini.addRule(RelativeLayout.ALIGN_PARENT_TOP);
miniLinearLayout.setLayoutParams(rpMini);
// 互动广告
LinearLayout interLinearLayout = new LinearLayout(ApplicationDemo.activity);
interLinearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
//interLinearLayout.setOrientation(LinearLayout.VERTICAL);
RelativeLayout.LayoutParams rpInter = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
rpInter.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
interLinearLayout.setLayoutParams(rpInter);
geInstance.loadInterAd(10,GEInter.INTERSPACEY,interLinearLayout);// 加载互动广告
ApplicationDemo.rlContainer.addView(miniLinearLayout);
ApplicationDemo.rlContainer.addView(interLinearLayout);

ApplicationDemo.lin.addView(ApplicationDemo.rlContainer);
}

2、ApplicationDemo.java
public class ApplicationDemo extends Cocos2dxActivity implements GEInfoListener {
private Cocos2dxGLSurfaceView mGLView;
public static FrameLayout lin;
public static LinearLayout container;
public static RelativeLayout rlContainer;
public static Activity activity;
public static ApplicationDemo applicationDemo;
//放在oncreate函数前面
static Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
CMAdvertisement.addAdvertisement();
break;
case 1:
CMAdvertisement.destroyAdvertisement();
break;
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// get the packageName,it's used to set the resource path
String packageName = getApplication().getPackageName();
super.setPackageName(packageName);

LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
lin = (FrameLayout) inflater.inflate(R.layout.helloworld_demo,null);
mGLView = (Cocos2dxGLSurfaceView) lin.findViewById(R.id.helloworld_gl_surfaceview);
mGLView.setTextField((Cocos2dxEditText) lin.findViewById(R.id.textField));
activity = this;
setContentView(lin);
}

public static void addAdvertisement() {
Message msg = Message.obtain();
msg.what = 0;
ApplicationDemo.mHandler.sendMessage(msg);
}

public static void destroyAdvertisement() {
Message msg = Message.obtain();
msg.what = 1;
ApplicationDemo.mHandler.sendMessage(msg);
}

protected void onPause() {
super.onPause();
mGLView.onPause();
}

protected void onResume() {
super.onResume();
mGLView.onResume();
}

static {
System.loadLibrary("Box2d");
System.loadLibrary("cocosdenshion");
System.loadLibrary("helloworld");
}
public void onInfoFail(String arg0) {
}

public void onInfoList(List<GEInfo> arg0) {
}
}

3、xml的添加方式:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainScreen"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<!--迷你广告-->
<LinearLayout
android:id="@+id/miniGELinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_alignParentTop="true"//设置在父节点上的未知
android:layout_marginTop="10dip"
/>
<!--互动广告-->
<LinearLayout
android:id="@+id/interGELinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dip"
/>
</RelativeLayout>

猜你在找的XML相关文章