首页 发现 嗨圈 新游 豆币商城
酷酷跑首页 > 游戏 > 单机 > 休闲 沙盒 > 我的世界完美版 > 我的世界完美版攻略 > 图片gui的制作(添加按钮)

图片gui的制作(添加按钮)

酷酷跑网友_86583 酷酷跑网友_86583 2016-05-20 02:27 发表
1138浏览 4回复 0收藏

要学gui,请不要问变量是啥

var ctx= com.mojang.minecraftpe.MainActivity.currentMainActivity.get()

获取一个主界面,必须的

ctx变量为一个主界面。

有主界面就可以进入线程了(线程是一个加快js运行并操控gui的必须)

ctx.runOnUiThread(newjava.lang.Runnable({run:function()

{

//线程内部

}}}));

然后就涉及到了显示gui的控件和布局

var ppw=newandroid.widget.PopupWindow();

//声明变量ppw为一个悬浮窗。可以理解为一个屏幕,可以显示东西

varlayout=newandroid.widget.RelativeLayout(ctx);

//声明变量layout为一个布局也就是一个排列东西的东西(默认为纵向)

如果i是一个按钮那么

i

i

i

如"图"三个按钮显示在布局里,这就是布局

有了以上资源我们认识一下像素以及他们的设置

不知哪位大大创造的自定义函数 像素

functiondip2px(ctx,dips){

return Math.ceil(dips*ctx.getResources().getDisplayMetrics().density);

}


dip2px(ctx,几像素 数字)

---------------------------------------------------------

dip2px(ctx,几像素 数字)调用方法

GUI就是刚才的悬浮窗ppw

GUI.setContentView()//设置GUI所展示的东西,()里填以后讲

GUI.setWidth();//宽

GUI.setHeight();//高

上两个应该填dip2px(ctx,几像素)

还有显示gui

GUI.showAtLocation(ctx.getWindow().getDecorView(),android.view.Gravity.左右|android.view.Gravity.上下,横偏移,纵偏移);

左右填LEFT是左 RIGHT是右,上下填TOP上或者BOTTOM下 大写

横偏移就是如果左右填左就是离左多少距离,填右就是…纵偏移以此类推

然后就进入我们的视图了

视图就是andriod安卓提供的控件比如按钮,文本,拖动条等等

先来按钮

varbutton=newandroid.widget.Button(ctx);

button.setText("X");


button.setOnClickListener(newandroid.view.View.OnClickListener({onClick:function(viewarg){


}}));

layout.addView(button);



别急我们来拆开分析

var空格button=new空android.widget.Button(ctx);

声明变量button是一个Button(按钮控件)

button.setText("x")

他的显示的文本是x

button.setOnClickListener(newandroid.view.View.OnClickListener({onClick:function(viewarg){

//点击按钮运行

}}));

layout.addView(button);

在layout上增加button控件

我们可以设置他

GUI.setContentView()//设置GUI所展示的

可以填button就是我们的按钮直接显示

也可以 GUI.setContentView(layout)

显示出布局都可以

正确例子

function dip2px(ctx,dips)

{

return Math.ceil(dips*ctx.getResources().getDisplayMetrics().density);

}

function newLevel(){

var ctx= com.mojang.minecraftpe.MainActivity.currentMainActivity.get()

ctx.runOnUiThread(new java.lang.Runnable({run:function()

{

try{

var simpleGUI=new android.widget.PopupWindow()

var layout=new android.widget.RelativeLayout(ctx);

var button=new android.widget.Button(ctx)

button.setText("X")

button.setOnClickListener(new android.view.View.OnClickListener({onClick:function(viewarg){

print("点击")

}}))

layout.addView(button)

simpleGUI.setContentView(layout)

simpleGUI.setWidth(70)

simpleGUI.setHeight(70)

simpleGUI.showAtLocation(ctx.getWindow().getDecorView(),android.view.Gravity.LEFT|android.view.Gravity.BOTTOM,0,0);

}

catch(err){print("Error:"+err)

}}}))


}

布局方向

layout.setOrientation(1)竖向

0为横向

GUI.setFocusable(true)是不是点击gui以外的地方gui消失


true是false不是

在例子中我们用了simpleGUI作为gui显示基础"屏幕",用layout显示了button按钮在simpleGUI上如此就成功了

而如何消除gui尼???就用到了GUI.dismiss。注意GUI是我们比喻的"屏幕"而不是按钮神马的

关于gui"屏幕"还有很多设置比如 GUI.setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(android.graphics.Color.argb(127,0,0,0)))设置背景颜色等等

回顾按钮 

var空格button=new空android.widget.Button(ctx);

声明变量button是一个Button(按钮控件)

button.setText("x")

他的显示的文本是x

button.setOnClickListener(new android.view.View.OnClickListener({onClick:function(viewarg){

//点击按钮运行

}}));

button.setTextColor(android.graphics.Color.rgb(1,1,1))

设置字体颜色

button.setBackgroundColor(android.graphics.Color.argb(127,251,251,251));

背景颜色


注color.rgb(,,)是调色分别填red(红) green(绿) blue(蓝)颜色

color.argb(,,,)填透明,红,绿,蓝

color.argb()和color.rgb()每个空可以填0到255调色

var stitle=new android.widget.TextView(ctx)

//把stitle赋值为TextView文本视图

stitle.setTextColor(android.graphics.Color.rgb(255,255,255))

//字体颜色,好像不能设置背景色

stitle.setText("文本")//文本

stitle.setTextSize(13)//字体大小

layout.addView(stitle)


//布局上增加

var edit=new android.widget.EditText(ctx)//输入框

edit.setTextColor(android.graphics.Color.rgb(1,1,1))//字体颜色

edit.setHint("请输入数字")//提示

edit.setInputType(android.text.InputType.TYPE_CLASS_NUMBER)//类型为数字不加这个为文本

edit.setText("0")//设置字

layout.addView(edit)//显示

var check=new android.widget.CheckBox(ctx);

check.setTextColor(android.graphics.Color.YELLOW);

check.setText("疾跑模式");

check.setChecked();//显是是否打对号填true,false或者一个布尔值变量

check.setOnCheckedChangeListener(new android.widget.CompoundButton.OnCheckedChangeListener(){

onCheckedChanged:function(v, isChecked){

//点击执行 isChecked是是否打对号布尔值 ,变量=isChecked可以作为这个的开关check.setChecked()里面填这个变量可以做到一个变量当开关

}});

layout.addView(check);

var ctx=com.mojang.minecraftpe.MainActivity.currentMainActivity.get()

var layout=new android.widget.LinearLayout(ctx)

try{

var menu=new android.widget.PopupWindow(layout, dip2px(ctx,75), dip2px(ctx,30));

menu.setFocusable(true)

var layout=new android.widget.LinearLayout(ctx)

layout.setOrientation(1)


var button=new android.widget.Button(ctx);

button.setText("确定");

button.setOnClickListener(new android.view.View.OnClickListener({

onClick:function(viewarg) {


}}));

layout.addView(button);


var mlayout=makeMenu(ctx,layout)

menu.setContentView(mlayout)

menu.setWidth(ctx.getWindowManager().getDefaultDisplay().getWidth()/3.3333333333);

menu.setHeight(ctx.getWindowManager().getDefaultDisplay().getHeight());


menu.setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(android.graphics.Color.argb(127,0,0,0)))

menu.showAtLocation(ctx.getWindow().getDecorView(),android.view.Gravity.RIGHT | android.view.Gravity.TOP,0,0);

}catch(err){

toast(err)

}


function makeMenu(ctx,layout){ 

var mlayout=new android.widget.RelativeLayout(ctx) 

var svParams=new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.FILL_PARENT,android.widget.RelativeLayout.LayoutParams.FILL_PARENT) 

var scrollview=new android.widget.ScrollView(ctx) 

var pad=dip2px(ctx,2) 

scrollview.setPadding(pad,pad,pad,pad) 

scrollview.setLayoutParams(svParams) 

scrollview.addView(layout) 

mlayout.addView(scrollview) 

return mlayout 

}

菜单少了像素函数

先看这个

var ctx=com.mojang.minecraftpe.MainActivity.currentMainActivity.get()

var layout=new android.widget.LinearLayout(ctx)

try{

var menu=new android.widget.PopupWindow(layout, dip2px(ctx,75), dip2px(ctx,30));

menu.setFocusable(true)

var layout=new android.widget.LinearLayout(ctx)

layout.setOrientation(1)


var button=new android.widget.Button(ctx);

button.setText("确定");

button.setOnClickListener(new android.view.View.OnClickListener({

onClick:function(viewarg) {


}}));

layout.addView(button);


var mlayout=makeMenu(ctx,layout)

menu.setContentView(mlayout)

menu.setWidth(ctx.getWindowManager().getDefaultDisplay().getWidth()/3.3333333333);

menu.setHeight(ctx.getWindowManager().getDefaultDisplay().getHeight());


menu.setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(android.graphics.Color.argb(127,0,0,0)))

menu.showAtLocation(ctx.getWindow().getDecorView(),android.view.Gravity.RIGHT | android.view.Gravity.TOP,0,0);

}catch(err){

toast(err)

}

大家能看懂吧 先看这个


var menu=new android.widget.PopupWindow(layout, dip2px(ctx,75), dip2px(ctx,30));

这个是我们比喻的"屏幕"


function makeMenu(ctx,layout){ 

var mlayout=new android.widget.RelativeLayout(ctx) 

var svParams=new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.FILL_PARENT,android.widget.RelativeLayout.LayoutParams.FILL_PARENT) 

var scrollview=new android.widget.ScrollView(ctx) 

var pad=dip2px(ctx,2) 

scrollview.setPadding(pad,pad,pad,pad) 

scrollview.setLayoutParams(svParams) 

scrollview.addView(layout) 

mlayout.addView(scrollview) 

return mlayout 

}

哪位大大设置只要

设置好布局layout后

var 变量=makeMenu(ctx,layout/*布局变量*/)

变量就好像加工过一样变成菜单


然后显示这个变量就可以了

大家不熟悉这个时候还是

var ctx=com.mojang.minecraftpe.MainActivity.currentMainActivity.get()

var layout=new android.widget.LinearLayout(ctx)

try{

var menu=new android.widget.PopupWindow(layout, dip2px(ctx,75), dip2px(ctx,30));

menu.setFocusable(true)

var layout=new android.widget.LinearLayout(ctx)

layout.setOrientation(1)

var button=new android.widget.Button(ctx);

button.setText("确定");

button.setOnClickListener(new android.view.View.OnClickListener({

onClick:function(viewarg) {

}}));

layout.addView(button);

布局添加地

var mlayout=makeMenu(ctx,layout)

menu.setContentView(mlayout)

menu.setWidth(ctx.getWindowManager().getDefaultDisplay().getWidth()/3.3333333333);

menu.setHeight(ctx.getWindowManager().getDefaultDisplay().getHeight());

menu.setBackgroundDrawable(new android.graphics.drawable.ColorDrawable(android.graphics.Color.argb(127,0,0,0)))

menu.showAtLocation(ctx.getWindow().getDecorView(),android.view.Gravity.RIGHT | android.view.Gravity.TOP,0,0);

}catch(err){

toast(err)

}

function makeMenu(ctx,layout){ 

var mlayout=new android.widget.RelativeLayout(ctx) 

var svParams=new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.FILL_PARENT,android.widget.RelativeLayout.LayoutParams.FILL_PARENT) 

var scrollview=new android.widget.ScrollView(ctx) 

var pad=dip2px(ctx,2) 

scrollview.setPadding(pad,pad,pad,pad) 

scrollview.setLayoutParams(svParams) 

scrollview.addView(layout) 

mlayout.addView(scrollview) 

return mlayout 

}

用这个比较好

我们只要把他放到自定义函数中

比如

var 菜单=function(){上面的菜单}

记得加上像素函数

就可以了

调用时 菜单() 就可以弹出菜单

var seekbar=new android.widget.SeekBar(ctx)//定义拖动条

seekbar.setMax(255)//拖动条长度

seekbar.setProgress(0)//显示时拖动到什么地方

//↓拖动时

seekbar.setOnSeekBarChangeListener(new android.widget.SeekBar.OnSeekBarChangeListener({

onProgressChanged:function(v){

seekbar.getProgress()//获取拖动位置

}}))


layout.addView(seekbar)//显示

还有选择框 var ll=new android.widget.Spinner(ctx)//定义

var k=new android.widget.ArrayAdapter(ctx,android.R.layout.preference_category,new java.lang.String("无,1").split(","))//适配器

ll.setOnItemSelectedListener(new android.widget.AdapterView.OnItemSelectedListener(){

onItemSelected:function(w){

ll.getSelectedItemId()//获取行数

}})

ll.setAdapter(k)//选择适配器

layout.addView(ll)//显示

对话框

var dialog=new android.app.AlertDialog.Builder(ctx)

dialog.setTitle("xx")//标题

dialog.setMessage("xx")//内容


dialog.setNegativeButton("按钮",new android.content.DialogInterface.OnClickListener(){

onClick: function(dia,w){

}})//增加一个按钮

dialog.show()


因为安卓控件都可以在layout布局上显示所以只要添加到布局就可以了

比如

var button=按钮控件

button.xxxx(xxx)设定完

layout.addView(button)

就显示了

然后用GUI"屏幕"显示它就可以了

控件.setVisibility(数字)

数字填0显示,填4不显示但占空间,填8完全不显示

bar=new android.widget.ProgressBar()

bar.setMax(最大)

bar.setProgress(进度)}

小伙伴们看不懂可以去黑暗时代嗨圈看mod制作教程,撒花  撒花

13个赞
0人打赏
0人转发

评论4

提交
酷酷跑网友_86583
2016-05-20 02:54
0 0人回复
我也说一句
回复
酷酷跑网友_86583
没人评论
2016-05-20 18:54
0 0人回复
我也说一句
回复
酷酷跑网友_86583
嗨起来
2016-05-20 23:52
0 0人回复
我也说一句
回复
酷酷跑网友_86583
顶顶顶
2016-05-21 16:03
0 0人回复
我也说一句
回复

推荐阅读

♥前言嗨!大家好,我是酷酷跑攻略写手爱脱机!今天为大家带来一期我的世界的新手进阶攻略,各位看官看好了!在《我的世界》中有很多东西是非常稀有的...
我的世界新手攻略
大家好,又见面了,我是六月今天,我来给大家介绍一下我的世界新手攻略一开始开始游戏,你的重生点就在你的脚下,如果要想更改重生点,就需要用到“床...
图片gui的制作(添加按钮)累计1138人浏览,4人评论,目前已经有6995用户在酷酷跑下载了我的世界完美版。
类型: 休闲 大小:72M 更新:2022-06-28
风靡世界的3D第一人称沙盘游戏

精选攻略

热门动态

相关文章