技术标签: Android
作为一个程序员,工作中经常需要写一些 demo 做测试,但是每次都要写布局和点击事件让人觉得厌烦。于是干脆直接写个基础类,想要什么直接动态添加到 View 中来。本来想利用 studio 模板语言 FreeMark 写一个模板的,但是 Android Studio 4.1 版本并不支持导入自定义模板,只能先把源代码放到博客上等用的时候直接复制了。
页面动态添加,设置由上到下,高度不限制,那么要添加 ScrollView + LinearLayout 组合;通过 id 获得主 View 对象,即可动态添加子视图。
https://gitee.com/ThornFUN/AndroidDemos/tree/master/DemoBase
`
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.Switch;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
/**
* 作者:lenovo
* 日期:2020/12/29 19:07
* 文件名:BaseDemoActivity_20201229.java
* 描述:优化配色,优化UI,不需要 layout 的引入;增加背景色
*/
public abstract class BaseDemoActivityNoColor_20201229 extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "DemoActivity";
private static final String COLOR_WHITE = "#FFFFFF";
private static final String COLOR_L_BLUE = "#99CCFF";
private static final String COLOR_D_BLUE = "#6699CC";
private static final String TAG_MAIN_LINEAR = "TAG_MAIN_LINEAR";
private LinearLayout mLinearMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scrollView = createMainView();
setContentView(scrollView);
mLinearMain = scrollView.findViewWithTag(TAG_MAIN_LINEAR);
initView();
}
/**
* 动态添加主视图
*
* @return
*/
public ScrollView createMainView() {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setTag(TAG_MAIN_LINEAR);
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(layoutParams);
linearLayout.setPadding(0, 0, 0, 200);
linearLayout.setOrientation(LinearLayout.VERTICAL);
ScrollView scrollView = new ScrollView(this);
scrollView.setLayoutParams(layoutParams);
scrollView.setFillViewport(true);
scrollView.addView(linearLayout);
return scrollView;
}
/**
* 初始化控件,可动态添加控件
* 例如:addText; addInputLinear; addButton; addResultView 等
*/
public abstract void initView();
/**
* 返回主 linearLayout 视图
*
* @return
*/
public LinearLayout getLinearMain() {
return mLinearMain;
}
/**
* 动态添加 button ,通过 tag 绑定点击事件
*
* @param btnText
* @param tag
* @return
*/
public Button addButton(String btnText, String tag) {
Button button = new Button(this);
button.setText(btnText);
button.setTextColor(Color.parseColor(COLOR_WHITE));
button.setBackgroundColor(Color.parseColor(COLOR_L_BLUE));
button.setTag(tag);
button.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
button.setHeight(100);
button.setOnClickListener(this);
mLinearMain.addView(button, getCommonParams());
return button;
}
public Button addButton(String btnTextAndTag) {
return addButton(btnTextAndTag, btnTextAndTag);
}
/**
* 动态添加 text ,用于设置测试案例标题
*
* @param text
* @return
*/
public TextView addText(String text) {
TextView textView = new TextView(this);
textView.setText(text);
textView.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
textView.setHeight(100);
textView.setGravity(Gravity.CENTER_VERTICAL);
mLinearMain.addView(textView, getCommonParams());
return textView;
}
/**
* 动态添加 textView,用于展示结果
*
* @param text
* @return
*/
@SuppressLint("ClickableViewAccessibility")
public TextView addResultView(String text) {
TextView textView = new TextView(this);
String pre = "运行结果如下:" + "\n";
textView.setText(String.format("%s%s", pre, text));
textView.setPadding(10, 30, 10, 30);
textView.setMovementMethod(ScrollingMovementMethod.getInstance());
textView.setBackgroundColor(Color.parseColor(COLOR_D_BLUE));
textView.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
textView.setHeight(500);
textView.setTextColor(Color.parseColor(COLOR_WHITE));
textView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
});
mLinearMain.addView(textView, getCommonParams());
return textView;
}
/**
* @param text
* @param resultView
* @return
*/
public boolean addResultText(String text, TextView resultView) {
if (resultView == null || TextUtils.isEmpty(text)) {
return false;
}
String oriText = resultView.getText().toString() + "\n";
resultView.setText(String.format("%s%s", oriText, text));
return true;
}
/**
* 动态添加 提示+输入框,用于数据输入
*
* @param inputDes
* @param isRequired
* @param length
* @return
*/
public EditText addInputLinear(String inputDes, boolean isRequired, int length) {
if (length < 0 || length == 0) {
length = -1;
}
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
// edit by lenovo on 2020/12/24 12:29 for 取消 edittext 自动聚焦
linearLayout.setFocusableInTouchMode(true);
TextView textView = new TextView(this);
StringBuilder sufText = new StringBuilder();
if (isRequired) {
sufText.append("(必填)");
}
if (length != -1) {
sufText.append("(").append(length).append("位长度)");
}
sufText.append(":");
textView.setText(String.format("%s%s", inputDes, sufText));
EditText editText = new EditText(this);
if (length != -1) {
editText.setFilters(new InputFilter[]{
new InputFilter.LengthFilter(length)});
}
editText.setMinHeight(100);
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 10, 20, 0);
LayoutParams layoutParams2 = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams2.setMargins(20, 0, 20, 10);
linearLayout.addView(textView, layoutParams);
linearLayout.addView(editText, layoutParams2);
mLinearMain.addView(linearLayout);
return editText;
}
public EditText addInputLinear(String inputDes, boolean isRequired) {
return addInputLinear(inputDes, isRequired, -1);
}
public EditText addInputLinear(String inputDes) {
return addInputLinear(inputDes, false);
}
/**
* 动态添加 提示 + 开关
*
* @param inputDes
* @param defaultState
* @return
*/
public Switch addSwitch(String inputDes, boolean defaultState) {
@SuppressLint("UseSwitchCompatOrMaterialCode") Switch switchView = new Switch(this);
switchView.setChecked(defaultState);
switchView.setText(inputDes);
switchView.setHeight(100);
mLinearMain.addView(switchView, getCommonParams());
return switchView;
}
/**
* 获取通用配置参数
*
* @return
*/
public LayoutParams getCommonParams() {
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 10, 20, 10);
return layoutParams;
}
@Override
public void onClick(View v) {
Object object = v.getTag();
if (!(object instanceof String)) {
return;
}
String tag = (String) v.getTag();
clickEvent(v, tag);
}
/**
* button 对应的点击事件,通过 tag 区分 button
*
* @param v view
* @param tag 标记
*/
public abstract void clickEvent(View v, String tag);
}
`
`
`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".BaseDemoActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linear_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</ScrollView>
</LinearLayout>
`
文章浏览阅读3.4k次。这里写目录标题主流方法图简单说明主流方法图简单说明从数据组织方法上看,主要有两种,一种是基于CHM,一种是直接基于点云。CHM实际上就是归一化的数字表面模型nDSM,实现方法为,建立二维规则格网,将点云投影到格网中,格网值为内部点云的最低点高程,格网中无点时,进行插值,即将邻近格网值赋予该无点格网,得到DEM,同理将最低点高程改为最高点高程得到DSM,二者做差即可得到CHM。a\ 分水岭方法在opencv库以及matlab中都可以直接调用分水岭函数来实现,关键在于如果在对应分割区域构建标记,来_单木分割
文章浏览阅读601次。为增进大家对内存的认识,本文将为大家介绍高频率内存的优势。此外,小编还将对虚拟内存加以探讨。我们每天都在同内存打交道,但大家对内存真的了解吗?上篇文章中,我们对服务器内存以及服务器内存技术有所介绍,为增进大家对内存的认识,本文将为大家介绍高频率内存的优势。此外,小编还将对虚拟内存加以探讨。如果你对内存及其相关知识具有兴趣,不妨继续往下阅读哦。一、高频率内存优势由于决定内存性能的核心因素有内存容量、..._服务器内存频率高有什么好处
文章浏览阅读3.6k次,点赞4次,收藏8次。报错内容:解决办法:给组件传入force-use-old-canvas="true"就可以在真机调试里展示了。重点:发布线上时,一定要将这句去掉,现在只是不支持真机调试,线上是可以使用的。重点:发布线上时,一定要将这句去掉,现在只是不支持真机调试,线上是可以使用的。重点:发布线上时,一定要将这句去掉,现在只是不支持真机调试,线上是可以使用的。<view style="width:..._html canvas is not defined
文章浏览阅读457次。计算机第一章计算机基础知识多项选择题(有两个或两个以上正确答案)1、下列说法中,正确的是________。A、一个汉字用1个字节表示 B、在微机中,使用最普遍的字符编码是ASCII码C、高级语言程序可以编译为目标程序 D、ASCII码的最高位用作奇偶校验位2、文件型(外壳型)计算机病毒主要感染扩展名为________。A、COM B、BAT C、EXE D、DOC3、..._达成某一任务的指令的会合称为语言
文章浏览阅读232次。说明SceneKit系列文章目录更多iOS相关知识查看github上WeekWeekUpProject本教程将包含以下内容:在SceneKit编辑器中建立基本的3D场景.编程加载并呈现3D场景.建立仿真物理,如何应用力.通过触摸与3D场景中的物体交互.设计并实现基本的碰撞检测.开始开始前,先下载初始项目starter project打开项目,简单查看一下里面都有些..._can knockdown1下载
文章浏览阅读1.3k次。材质和Unity Shader在unity中,需要配合使用材质Material和Unity Shader才能达到需要的效果流程创建一个材质→创建一个Unity Shader,并把它赋给上一步中创建的材质→把材质赋给要渲染的对象→在材质面板中调整Unity Shader的属性,以得到满意的效果Unity中的材质Unity中的材质需要配合一个GameObject的Mesh或者Particle Systen组件来工作,它决定了我们的游戏对象看起来是什么样子的Unity中的Shader为了和前面通用的_unity的扩展sprites的shader
文章浏览阅读844次。一、QTextCodecQTextCodec类提供了文本编码转换功能。指定字符集对文本进行转换。1.Qt程序中所有要显示到界面上的字符串最好都是用tr()函数;代码如下(示例):QTextCodec::setCoderForTr(QTextCodec::codecForName("UTF-8"));QLabel label;label.setText(QObject::tr("你好,世界!"));2.对于不是要显示到界面上的字符串中如果包含了中文,可以使用QString()进行编码转换。代_qt 将txt文件转换成字符串
文章浏览阅读2k次。error: failed to push some refs to 'https://gitee.com/liyue25/test.git'To https://gitee.com/liyue25/test.githint: Updates were rejected because the remote contains work that you dohint: not have locally. This is usually caused by another repository push_the breanch to pull from should be selected
文章浏览阅读94次。写一个自己的rjust函数,创建一个字符串的长度是指定长度,原字符串在新字符串中右对齐,剩下的部分用指定的字符填充。写一个自己的index函数,统计指定列表中指定元素的所有下标,如果列表中没有指定元素返回-1。写一个自己的replace函数,将指定字符串中指定的旧字符串转换成指定的新字符串。编写一个函数,提取指定字符串中所有的字母,然后拼接在一起产生一个新的字符串。写一个自己的endswith函数,判断一个字符串是否已指定的字符串结束。写一个自己的upper函数,将一个字符串中所有的小写字母变成大写字母。._写一个自己的rjust函数,创建一个字符串的长度是指定长度,原字符串在新字符串中右
文章浏览阅读1.3k次。spring自定义全局异常背景学习目标案列背景在springmvc的controller中程序员经常要封装错误对象返回错误,前端显示错误文案。通过spring的异常处理器,来进行自定义异常处理学习目标学习全局异常拦截器处理controller所有的异常的返回封装定义枚举异常码,设计自定义异常案列创建枚举异常码,定义系统异常情况/** * @author tianjz */..._18jzz大全
文章浏览阅读95次。越来越多的人搞爬虫,设计到多线程爬取, 还有一些机器学习的一些模块也需要这玩意, 感觉自己不会逼格不高. 抽时间赶紧玩一玩这东西, 希望提高对软件的认知和归属感,不要太傻.cpu内部架构参考你知道CPU是如何工作的?-视频CPU核心越多越好?你的CPU可能正在养老!你真的了解多核处理器吗?1.双核≠双性能多核不一定会使你的手机或电脑速度更快,但它将提高你的PC的整体性能,这是一个...
文章浏览阅读2k次,点赞5次,收藏18次。本篇博客介绍如何利用XBee模块实现QGC地面站与飞控的通信_xbee实现多机控制