技术标签: Android基础UI
自己写layout,用LayoutInflater进行动态加载,然后用setContentView进行添加。
layout方面,对title还有按键以及对话框整体进行了设置,通过background引用drawable中相应的xml文件实现调整。
//布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:gravity="center_vertical"
android:layout_height="match_parent"
android:background="@drawable/dialog_background">
<TextView
android:id="@+id/text_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/myTextView"
/>
<TextView
android:id="@+id/text_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是内容"
android:textAppearance="@android:style/TextAppearance.Medium"
android:layout_gravity="center_horizontal"
android:layout_margin="15dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<Button
android:id="@+id/button_n"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="@android:style/TextAppearance.Medium"
android:background="@drawable/btn_left_background"
android:text="取消"/>
<Button
android:id="@+id/button_y"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="@android:style/TextAppearance.Medium"
android:text="确定"
android:background="@drawable/btn_right_background"
/>
</LinearLayout>
</LinearLayout>
第一个TestView引用了styles中的布局,这种方法通常用来存放比较常用的布局,以便重复使用。
//styles中对应的布局myTestView
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style name="NoDialogTitle" parent="@android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
<style name="myTextView">
<item name="android:background">@drawable/title_background</item>
<item name="android:textAppearance">@android:style/TextAppearance.Large</item>
<item name="android:textColor">@color/blue</item>
</style>
</resources>
其中的名为NoDialogTitle的设置是老师给的去除边框圆角以外的部分的,引用的时候在代码中这样引用Dialog dialog=new Dialog(MainActivity.this,R.style.NoDialogTitle);
//drawable中的xml文件
//title
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners
android:topLeftRadius="@dimen/corners"
android:topRightRadius="@dimen/corners" />
<solid android:color="#0D7A29" />
/>
</shape>
//对话框整体
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="@dimen/corners" />
<solid android:color="#ffffff" />
<stroke
android:width="1dp"
android:color="#64251200" />
</shape>
//右键
//按下去的时候
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="@dimen/corners" />
<solid android:color="#ffffff" />
<stroke
android:width="1dp"
android:color="#64251200" />
</shape>
//平常的时候
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:bottomRightRadius="@dimen/corners" />
<solid android:color="#ffffff" />
<stroke
android:width="1dp"
android:color="#64251200" />
</shape>
//设置右键按键
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_right_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/btn_right_normal"/>
</selector>
//左键雷同设置,只是圆角设置为bottomLeftRadius
shape中颜色solid,边框stroke,圆角conrners
活动和昨天的一样,只是添加了一个Button来触发而已,具体的自定义方法见下面代码
//Activity
private void userDefinedDialog() {
// 应用自己设置的主题
dialog=new Dialog(MainActivity.this,R.style.NoDialogTitle);
// 获得并设置自定义Dialog
LayoutInflater inflater=getLayoutInflater();
View userDefinedDialog=inflater.inflate(R.layout.user_defined_dialog_layout, null);
TextView textView_title= (TextView) userDefinedDialog.findViewById(R.id.text_title);
TextView textView_message= (TextView) userDefinedDialog.findViewById(R.id.text_message);
Button button_y= (Button) userDefinedDialog.findViewById(R.id.button_y);
Button button_n= (Button) userDefinedDialog.findViewById(R.id.button_n);
Spanned spanned= Html.fromHtml("<img src =''/>这是一个新的<font color=#ff0000>标题</font>", new Html.ImageGetter() {
@Override
public Drawable getDrawable(String source) {
Drawable drawable=getResources().getDrawable(R.mipmap.ic_launcher);
drawable.setBounds(0,0,drawable.getIntrinsicHeight(),drawable.getIntrinsicWidth());
return drawable;
}
},null);
textView_title.setText(spanned);
textView_message.setText("这是一个新的内容");
button_y.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "点击确定", Toast.LENGTH_SHORT).show();
// 关闭dialog
dialog.dismiss();
}
});
button_n.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
// 去掉原有标题
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// 添加自己的View到dialog
dialog.setContentView(userDefinedDialog);
dialog.setCancelable(false);
dialog.show();
}
活动和上面的自定义Dialog一样,只是加了个button触发,具体代码实现如下。构造对象时DatePickerDialog(Context context, int theme, DatePickerDialog.OnDateSetListener listener, int year, int monthOfYear, int dayOfMonth)依次传入上下文,监听器,年,月,日。
下面的Demo中通过Calendar获得当前时间,将当前时间传入Dialog中。监听器中通过int year, int monthOfYear, int dayOfMonth监听Dialog的具体数据,并将数据传入Calendar中,通过getTime方法设置年月日,其中有用到SimpleDateFormat 造型。Calender.getTime()获得的是当前的时间,如果不加设置的话显示的就是当前时间。因为这里只设置了年月日,如果显示的时候加上小时分钟,小时分钟就是getTime得到的当前时间。这一点下面的TimePickerDialog中也一样,TimePickerDialog的例子中并没有设置年月日,但是显示的时候还是出现了当前的年月日。
private void dataPickerDialog() {
Calendar mCalendar=Calendar.getInstance();
DatePickerDialog datePickerDialog=new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mCalendar.set(year, monthOfYear, dayOfMonth);
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日");
// simpleDateFormat format(data):将data类型的值造型成"yyyy年MM月dd日",calecdar.getTime()返回data类型
Toast.makeText(getApplication(),"这个造型是"+simpleDateFormat.format(mCalendar.getTime()),Toast.LENGTH_SHORT).show();
}
},mCalendar.get(Calendar.YEAR),mCalendar.get(Calendar.MONTH),mCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.show();
}
对象的构造TimePickerDialog(Context context, TimePickerDialog.OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView),上下文,监听器,小时,分钟,最后一个参数,true对应24小时,false对应12小时
Calendar mCalendar=Calendar.getInstance();
TimePickerDialog dialog=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mCalendar.set(mCalendar.HOUR,hourOfDay);
mCalendar.set(mCalendar.MINUTE,minute);
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日HH:mm");
simpleDateFormat.format(mCalendar.getTime());
// 因为getTime返回的是现在这一时刻距离某年到现在的时间。所以除了规定传入的时间外,其余时间与手机同步
Toast.makeText(getApplicationContext(),"时间为"+simpleDateFormat.format(mCalendar.getTime()),Toast.LENGTH_SHORT).show();
}
},mCalendar.get(Calendar.HOUR),mCalendar.get(Calendar.MINUTE),true);
// },5,6,true);
dialog.show();
}
Dialog中有默认的布局,PopupWindow中没有默认的布局,必须自己设置。Demo中有一个onKeyDown方法,这个是手机按键的监听器,这里设置back的监听:PopupWindow运行的时候,点击back键关闭PopupWindow。注意加上PopupWindow为空是时候。具体使用方法见代码
//活动
public class MainActivity extends Activity implements View.OnClickListener{
private Button mBtn_pop;
private PopupWindow mPopupWindow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtn_pop= (Button) findViewById(R.id.button_pop);
mBtn_pop.setOnClickListener(this);
}
// 监听手机按键
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK){
// 必须用短路与,如果mPopupWindow是null直接跳出,不再执行后面的判断条件
if(mPopupWindow!=null&&mPopupWindow.isShowing()){
mPopupWindow.dismiss();
// return true后这个方法内下面的语句就不执行了。
// 不返回true的话按back键会继续执行下面的语句,退出活动
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_pop:
popupWindow();
break;
default:
break;
}
}
private void popupWindow() {
mPopupWindow=new PopupWindow(MainActivity.this);
// 设置宽高
mPopupWindow.setHeight(ActionBar.LayoutParams.WRAP_CONTENT);
mPopupWindow.setWidth(ActionBar.LayoutParams.MATCH_PARENT);
// 添加自定义view
View popupView=getLayoutInflater().inflate(R.layout.my_popupwindow,null);
mPopupWindow.setContentView(popupView);
// 点击Window以外的界面返回,必须在show之前设置
mPopupWindow.setOutsideTouchable(true);
// 在指定按键下面显示
mPopupWindow.showAsDropDown(mBtn_pop);
}
}
//main_activity
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<Button
android:id="@+id/button_pop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="PopupWindow测试"/>
</RelativeLayout>
//自定义的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="center_horizontal"
android:text="文本1"/>
<TextView
android:id="@+id/text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="center_horizontal"
android:text="文本2"/>
<TextView
android:id="@+id/text_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="center_horizontal"
android:text="文本3"/>
</LinearLayout>
手机上接收到的一些推送就是由这个实现的。NotificationManager 控制Notification的发送。PendingIntent 设置弹出界面的链接。
//活动
public class MainActivity extends Activity implements View.OnClickListener{
private Button mBtn1;
private Button mBtn2;
private Button mBtn3;
private NotificationManager mNotificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtn1= (Button) findViewById(R.id.button_sendnew);
mBtn3= (Button) findViewById(R.id.button_sendold);
mBtn2= (Button) findViewById(R.id.button_cancel);
// 初始化notificationmanager
mNotificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this);
mBtn3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_sendnew:
newNotification();
break;
case R.id.button_sendold:
oldNotification();
break;
case R.id.button_cancel:
mNotificationManager.cancel(1);//取消id为1的通知
break;
default:
break;
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void newNotification() {
Intent intent=new Intent(getApplicationContext(),MainActivity.class);//设置pendingintent事件
// 设置pendingintent使用方式
PendingIntent pend=PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_ONE_SHOT);
Notification notification=new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher).setTicker("我是一条消息")
.setContentTitle("我是一个标题").setContentText("我是一个文本").setContentInfo("我是内容").setContentIntent(pend)
.setAutoCancel(true).setWhen(System.currentTimeMillis()).getNotification();//build
mNotificationManager.notify(1,notification);
}
private void oldNotification() {
Notification notification=new Notification();//初始化notification
notification.icon= R.mipmap.ic_launcher;//设置通知的图片
notification.tickerText="我是一个消息";//设置状态栏文本
notification.flags=Notification.FLAG_AUTO_CANCEL;//设置为可以取消
Intent intent=new Intent(getApplicationContext(),MainActivity.class);//设置pendingintent事件
// 设置pendingintent使用方式
PendingIntent pend=PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_ONE_SHOT);
// 设置pendingIntent显示的内容
notification.setLatestEventInfo(getApplicationContext(),"我是标题","我是内容",pend);
notification.when=System.currentTimeMillis();//设置触发时间or Calender.getInstance().getTimeInMillis()
mNotificationManager.notify(1,notification);//通知管理器将id为1的通知添加
}
}
//布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/button_sendold"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送消息(旧方法)" />
<Button
android:id="@+id/button_sendnew"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送消息(新方法)" />
<Button
android:id="@+id/button_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消消息" />
</LinearLayout>
《Java和Android开发实战详解》《Java和Android开发实战详解》把Java和Android开发技术结合起来讲解,可以使读者更快适应Android的开发。全书包括18章:第1章Java结构化程序设计,介绍了安装JDK和Eclipse IDE来建立Java开发环境;第2章详细说明Java程序的基本架构;第3章~第6章是Java语言开发的技术,以便帮助
对EXT,下载一个ext2.0的API文件,叫你如何看懂API,看完保证就会用API,但是你必须看完。Config Options Public Properties Public Methods Public Events基本上都由以上4部分组成Config Options下的内容为你在实例化一个对象时进行配置,也就是比如new Panel({a:"xxx",...
jhat中的OQL(对象查询语言)如果需要根据某些条件来过滤或查询堆的对象,这是可能的,可以在jhat的html页面中执行OQL,来查询符合条件的对象基本语法:select [from [instanceof] ][where ]解释:(1)class name是Java类的完全限定名,如:java.lang.String, java.util.ArrayList, [C是char数组, [Lj...
u-boot FIT image介绍作者:wowo 发布于:2016-9-2 21:49 分类:u-boot分析1. 前言Linux kernel在ARM架构中引入device tree(全称是flattened device tree,后续将会以FDT代称)的时候[1],其实怀揣了一个Unify Kernel的梦想----同一个Image,可以支持多个不同的平台。随着新的ARM64架
1.概述在进行数据传输中,批量加载数据到HBase集群有多种方式,比如通过HBase API进行批量写入数据、使用Sqoop工具批量导数到HBase集群、使用MapReduce批量导入等。这些方式,在导入数据的过程中,如果数据量过大,可能耗时会比较严重或者占用HBase集群资源较多(如磁盘IO、HBase Handler数等)。今天这篇博客笔者将为大家分享使用HBase BulkLoad的方式来进行海量数据批量写入到HBase集群。2.内容在使用BulkLoad之前,我们先来了解一下HBase的存储机
JS语法:浮点数是二进制算法,能精确表示分数1/2,1/16等,所以(0.3-0.2) != (0.2-0.1)。(解决方法:*10再/10)标签语句:给break和continue增加标签,可以实现类似goto的效果。可以给函数定义属性,当函数需要一个"静态变量",可以给函数一个属性为该变量,这样就避免了杂乱无章的全局变量给sort()传一个函数参数:function create...
红米8A时32位的处理器,所以需要修改一下Magisk的源码才可以正常刷入如何修改Magisk 源码呢?boot_patch.sh脚本 if [ "$ARCH" = "arm" ]; then ui_print "- Patch arm32 kernel" . ./patch_kernel.sh [ "$?" -eq 0 ] && ui_print "- Patch arm32 kernel success" || ui_print "- Patch a
linux 使用wineIf you use Linux, you probably know about WINE. This handy program can run many different kinds of Windows programs. I personally use it to run Office (but be careful; not all versions wor...
样条插值法在实际的数学建模问题中,高次的样条插值多项式也会产生“龙格现象”,因此,常常使用“三次样条插值”来提高“模拟数据”的准确性。三次样条插值三次样条插值法的代码实现:%三次样条插值法a=0;a=input('请输入数据矩阵的行数:');b=0;b=input('请输入数据矩阵的列数:');%初始化目标矩阵c=zeros(a,b);c=input('请依次输入数据矩阵:');disp('数据矩阵:');disp(c);%确定插值区间d=0;d=input('请输
什么是SEO什么是搜索引擎优化SEO是SearchEngineOptimization)翻译为搜索引擎优化.搜索引擎优化(SearchEngineOptimization,简称SEO)是一种利用搜索引擎的搜索规则来提高目的网站在有关搜索引擎内的排名的方式SEO搜索引擎优化是什么意思?seo搜索引擎优化,就是利用搜索引擎的一些规则将自己的网站做到自然排名靠前(就是你随便搜索一个东西,不带推广字眼,有快照的就是自然排名).排名靠前了,就会获得更大的流量,当别人搜索的时候,比如情调主题餐厅,别人就进入
目录概念外键约束外键约束的特点添加数据在外键约束下的数据操作删除外键约束多对多操作案例多表查询交叉连接内连接查询外连接查询子查询子查询关键字-ALL️子查询关键字-ANY和SOME子查询关键字-IN️子查询关键字-EXISTS3自关联查询总结每文一语概念实际开发中,一个项目通常需要很多张表才能完成例如:一个商城项目就需要分类表(category)、商品表(products).
最近有OBS开发的需求, 难免需要阅读理解源码. 所幸不需要过多的去阅读ffmpeg的代码, 这部分我也不会~主要还是QT的代码. 记录下OBS源设置过程中几个重要的类.OBSBasicSourceSelectOBSBasicPropertiesOBSPropertiesView这三个类在代码里就完成了对OBS源的添加设置等工作. 不过还有一些辅助的类, 主要是操作配置