android:gravity="center_horizontal|center_vertical"

本文深入探讨Java中的代理机制,包括静态代理StaticProxy和动态代理DynamicProxy,并通过具体示例展示了如何利用代理机制分离核心业务逻辑与辅助功能逻辑。

可以说代理是java十分重要的一种机制,另一个当然是属于反射了,jdk中单独讲到了反射API(java.lang.reflect),可能有人认为反射对资源消耗比较厉害,确实也是,反射肯定是要消耗资源的,但也不是什么都要用到反射,所以最佳试验应该是在资源消耗程度和反射的使用程度之间找到一个平衡点,本文并不打算讲反射,关于反射的心得以后再贴出来,自己最近也在折磨折磨

代理可以分为:StaticProxy 和DynamicProxy
比如:

Java代码 复制代码
  1. Package xyz;    
  2. import java.util.logging.*    
  3. public class talkToSomebody{    
  4. private Logger logger=Logger.getLogger(this.getClass().getName());    
  5. public void talk(String name){    
  6. logger.log(Level.INFO,"talking start....");    
  7. System.out.println("Hi!ni hao,"+name);    
  8. logger.log(Level.INFO,"talking ends....");    
  9. }    
  10. }   
Package xyz; 
import java.util.logging.* 
public class talkToSomebody{ 
private Logger logger=Logger.getLogger(this.getClass().getName()); 
public void talk(String name){ 
logger.log(Level.INFO,"talking start...."); 
System.out.println("Hi!ni hao,"+name); 
logger.log(Level.INFO,"talking ends...."); 
} 
} 


很显示,你需要talk其他人,其实就只有一个句话是关键的,"Hi!ni hao XXX" ,这才是需要关系的,或者叫核心业务(这个次可能有点牵强),但如果要记录你和哪些人交谈过,哪时候开始的,哪时候结束的,日志功能就是实现这个,这属于业务逻辑,把业务逻辑和核心业务放到了一起,如果哪天不需要记录了,怎么办?得重新改源代码,甚至如果客户只提供给你编译过的class或接口,你会很郁闷的!

解决方法:
用Proxy机制,其实代理就像一个中介机构,我自己突然有什么事(或者不愿意),找中介机构去做,当然你得出钱给中介机构。

Java代码 复制代码
  1. public interface ITalk{    
  2. public void talk(String name);    
  3. }   
public interface ITalk{ 
public void talk(String name); 
} 



可以把这个看做你的要求,中介机构必须按照你的要求来做,你才会付钱给中介机构;

Java代码 复制代码
  1. public class TalkToSomebody implements ITalk{    
  2. public void talk(String name){    
  3. System.out.println("Hi,ni hao,"+name);    
  4. }    
  5. }   
public class TalkToSomebody implements ITalk{ 
public void talk(String name){ 
System.out.println("Hi,ni hao,"+name); 
} 
} 

不错,中介机构是按照我的要求实现的,结果没错!

Java代码 复制代码
  1. public class StaticProxyTalk Implements ITalk{    
  2. private Logger logger=Logger.getLogger(this.getClass().getName());    
  3. private ITalk somebody;    
  4. public StaticProxyTalk(ITalk somebody){    
  5. this.somebody=somebody;    
  6. }    
  7. public void talk(String name){    
  8. log("talking start....");    
  9. somebody.talk(name);    
  10. log("talking ending...");    
  11. }    
  12. private void log(String message){    
  13. logger.log(Level.INFO,message)    
  14. }   
public class StaticProxyTalk Implements ITalk{ 
private Logger logger=Logger.getLogger(this.getClass().getName()); 
private ITalk somebody; 
public StaticProxyTalk(ITalk somebody){ 
this.somebody=somebody; 
} 
public void talk(String name){ 
log("talking start...."); 
somebody.talk(name); 
log("talking ending..."); 
} 
private void log(String message){ 
logger.log(Level.INFO,message) 
} 



感觉好多了,以后我不需要中介服务了,不去找他就行,现在看下这个中介机构做得怎么样,达到我的要求了没?

Java代码 复制代码
  1. public class TestProxy{    
  2. public static void main(String []args){    
  3. ITalk proxy=new StaticProxyTalk(new TalkToSomebody());    
  4. proxy.talk("HuYong");    
  5. }    
  6. }   
public class TestProxy{ 
public static void main(String []args){ 
ITalk proxy=new StaticProxyTalk(new TalkToSomebody()); 
proxy.talk("HuYong"); 
} 
} 


是的,,它做到了,我可以付钱给它了。。

但是问题还是存在,如果我有N多事都不想自己做(比较懒),我得每一件都去找中介机构吗?能不能一类的就找一次就够了勒??

看下面的一个LogTalk:

Java代码 复制代码
  1. import java.lang.reflect.InvocationHandler;    
  2. import java.lang.reflect.Method;    
  3. import java.lang.reflect.Proxy;    
  4. import java.util.logging.Level;    
  5. import java.util.logging.Logger;    
  6.   
  7. /**   
  8. * @author HuYong Email:yate7571@hotmail.com   
  9. */    
  10. public class LogTalk implements InvocationHandler {    
  11. private Logger logger = Logger.getLogger(this.getClass().getName());    
  12.   
  13. private Object object;    
  14.   
  15. public Object bind(Object object) {    
  16. this.object = object;    
  17. return Proxy.newProxyInstance(object.getClass().getClassLoader(),    
  18. object.getClass().getInterfaces(), this);    
  19.   
  20. }    
  21.   
  22. public Object invoke(Object proxy, Method method, Object[] args)    
  23. throws Throwable {    
  24. Object result = null;    
  25. try {    
  26. log("method starts ...." + method);    
  27. result = method.invoke(object, args);    
  28. log("method ends...." + method);    
  29. catch (Exception e) {    
  30. log(e.toString());    
  31. }    
  32. return result;    
  33. }    
  34.   
  35. private void log(String message) {    
  36. logger.log(Level.INFO, message);    
  37. }    
  38.   
  39. }   
import java.lang.reflect.InvocationHandler; 
import java.lang.reflect.Method; 
import java.lang.reflect.Proxy; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

/** 
* @author HuYong Email:yate7571@hotmail.com 
*/ 
public class LogTalk implements InvocationHandler { 
private Logger logger = Logger.getLogger(this.getClass().getName()); 

private Object object; 

public Object bind(Object object) { 
this.object = object; 
return Proxy.newProxyInstance(object.getClass().getClassLoader(), 
object.getClass().getInterfaces(), this); 

} 

public Object invoke(Object proxy, Method method, Object[] args) 
throws Throwable { 
Object result = null; 
try { 
log("method starts ...." + method); 
result = method.invoke(object, args); 
log("method ends...." + method); 
} catch (Exception e) { 
log(e.toString()); 
} 
return result; 
} 

private void log(String message) { 
logger.log(Level.INFO, message); 
} 

} 


只要我需要做的事是一类事(可以理解一类事物),我就可以先和中介机构签好活动,我以后所有的你帮我做就是了,我只需要结构就ok了,中介机构也承诺,只要你给我们的都符合这个约定(都是Object),我就接了。

也来测试下:

Java代码 复制代码
  1. public class TestDynamicProxy{    
  2. public static void main(String []args){    
  3. LogTalk dynamicproxy=new LogTalk();    
  4. ITalk proxy=(ITalk)dynamicproxy.bind(new TalkToSomebody());    
  5. proxy.talk("YangYi");    
  6. }    
  7. }   
public class TestDynamicProxy{ 
public static void main(String []args){ 
LogTalk dynamicproxy=new LogTalk(); 
ITalk proxy=(ITalk)dynamicproxy.bind(new TalkToSomebody()); 
proxy.talk("YangYi"); 
} 
} 


可以通过了,,以后这些事你都可以帮我做了,我列个清单给中介机构有哪些事了,这些事你就帮我做了,如果哪天有事不需要做了,我打电话给你取消那项就可以了,不影响其他事情的继续做下去,也不需要去改动相关的约定了。。


关于LogTalk的讲解:

Java代码 复制代码
  1. public static Object newProxyInstance(ClassLoader loader,    
  2. Class<?>[] interfaces,    
  3. InvocationHandler h)    
  4. throws IllegalArgumentException   
public static Object newProxyInstance(ClassLoader loader, 
Class<?>[] interfaces, 
InvocationHandler h) 
throws IllegalArgumentException 

返回一个指定接口的代理类实例,该接口可以将方法调用指派到指定的调用处理程序

Java代码 复制代码
  1. public Object bind(Object object) {    
  2. this.object = object;    
  3. return Proxy.newProxyInstance(object.getClass().getClassLoader(),    
  4. object.getClass().getInterfaces(), this);   
public Object bind(Object object) { 
this.object = object; 
return Proxy.newProxyInstance(object.getClass().getClassLoader(), 
object.getClass().getInterfaces(), this); 


绑定,只要是Object的子类就可以绑定(呵呵,所有的都是Object的子类勒!)

总结:这其实是AOP的最底层实现,AOP的的好处就是用到了代理,把各种业务逻辑分离开来了,不管是核心要处理的还是作为辅助功能(或者测试)的业务逻辑,比如日志作为一个切面可以去测试每个方法是否都执行了,用AOP就不需要去改动任何核心业务,如果不要了,就不指定Pointcut就可以了(关于AOP的各种术语可以参考 spring reference),这应该算是一种思想的转变。

补充:可能我用到的"核心业务",和其他"业务逻辑"理解有点不同,个人理解是这样的:核心业务就是我需要去关心的,这是核心。其他业务逻辑(很多书上说是与业务逻辑无关的系统服务逻辑)比如说日志,安全方面等,只是做为核心的一个外壳,没有外壳核心照样可以存活,只是没有那么美观了。

<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true"> <LinearLayout android:id="@+id/MainPage" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <RelativeLayout android:id="@+id/SerialContent" android:layout_width="match_parent" android:layout_height="60dp"> <TextView android:id="@+id/Serial" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="串口号" /> <Spinner android:id="@+id/SerialName" android:layout_width="100dp" android:layout_height="80dp" android:gravity="center" android:spinnerMode="dropdown" android:layout_toRightOf="@+id/Serial"/> <Spinner android:id="@+id/BaudRate" android:layout_width="140dp" android:layout_height="80dp" android:layout_toRightOf="@id/SerialName" android:gravity="center" android:spinnerMode="dropdown" /> <Button android:id="@+id/OpenSerial" android:layout_width="120dp" android:layout_height="match_parent" android:layout_toRightOf="@id/BaudRate" android:text="打开串口" /> <Button android:id="@+id/QuitSoftware" android:layout_width="140dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:text="退出软件" /> </RelativeLayout> <FrameLayout android:id="@+id/previewContent" android:layout_width="604.7dp" android:layout_height="430dp" android:visibility="gone"> <androidx.camera.view.PreviewView android:id="@+id/preview" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> <RelativeLayout android:id="@+id/handleContent" android:layout_width="match_parent" android:layout_height="430dp" android:visibility="visible"> <TextView android:id="@+id/handle" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="手柄" /> <LinearLayout android:id="@+id/RightContent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_toRightOf="@+id/handle" android:orientation="vertical"> <RelativeLayout android:id="@+id/content1" android:layout_width="match_parent" android:layout_height="60dp" android:layout_toRightOf="@+id/handle"> <Button android:id="@+id/ChangeDial" android:layout_width="140dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:text="带拨号盘版" /> </RelativeLayout> <LinearLayout android:id="@+id/DialContent" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <EditText android:id="@+id/receivedDataView" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@android:drawable/editbox_background" android:focusable="false" android:focusableInTouchMode="false" android:cursorVisible="false" android:gravity="top|left" android:inputType="textMultiLine" android:padding="8dp" android:scrollbars="vertical" android:hint="等待数据..." android:textSize="14sp" /> <LinearLayout android:id="@+id/mianBoard" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical"> <LinearLayout android:id="@+id/control" android:layout_width="match_parent" android:layout_height="74dp" android:orientation="horizontal" android:visibility="visible"> <EditText android:id="@+id/commandInput" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:hint="输入指令" android:paddingLeft="10dp" android:inputType="text" android:maxLines="1" /> <Button android:id="@+id/sendButton" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="发送" /> <Button android:id="@+id/clearDataView" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="清空" /> </LinearLayout> <GridLayout android:id="@+id/keyBoard" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="4" android:columnCount="3" android:rowCount="4" android:visibility="visible"> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="1" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="2" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="3" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="4" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="5" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="6" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="7" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="8" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="9" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="*" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="0" /> <Button android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="#" /> </GridLayout> <GridLayout android:id="@+id/NoKeyBoard" android:layout_width="match_parent" android:layout_height="0dp" android:columnCount="6" android:rowCount="4" android:layout_weight="5" android:visibility="gone"> <TextView android:id="@+id/keyA" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEYA" android:gravity="center"/> <Button android:id="@+id/keyAStart" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1.2" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/keyAClose" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1.2" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/keyB" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEYB" android:gravity="center"/> <Button android:id="@+id/keyBStart" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1.2" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/keyBClose" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1.2" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/key1" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEY1" android:gravity="center"/> <Button android:id="@+id/key1Start" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/key1Close" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/key2" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEY2" android:gravity="center"/> <Button android:id="@+id/key2Start" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/key2Close" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/key3" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEY3" android:gravity="center"/> <Button android:id="@+id/key3Start" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/key3Close" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/key4" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEY4" android:gravity="center"/> <Button android:id="@+id/key4Start" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/key4Close" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="关灯" /> <TextView android:id="@+id/key5" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="KEY5" android:gravity="center"/> <Button android:id="@+id/key5Start" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="开灯" /> <Button android:id="@+id/key5Close" android:layout_width="0dp" android:layout_height="0dp" android:layout_columnWeight="1" android:layout_rowWeight="1" android:text="关灯" /> </GridLayout> </LinearLayout> </LinearLayout> </LinearLayout> </RelativeLayout> <RelativeLayout android:id="@+id/VoiceContent" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/voice" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="录音" /> <Button android:id="@+id/RecordingVoice" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:layout_toRightOf="@+id/voice" android:text="开始录音" /> <TextView android:id="@+id/ShowVoiceData" android:layout_width="150dp" android:layout_height="match_parent" android:layout_toRightOf="@+id/RecordingVoice" android:background="@drawable/solid_shape" android:gravity="center" android:ellipsize="end" android:singleLine="false" android:maxLines="3"/> <Button android:id="@+id/PlayVoice" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:layout_toRightOf="@id/ShowVoiceData" android:text="播放语音" /> <Button android:id="@+id/DeleteAudioAndView" android:layout_width="150dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_margin="2dp" android:text="删除" /> </RelativeLayout> <RelativeLayout android:id="@+id/CameraContent" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/camera" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="摄像头" /> <Button android:id="@+id/OpenAndCloseCamera" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:layout_toRightOf="@+id/camera" android:text="打开摄像头" /> <Button android:id="@+id/photograph" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:layout_toRightOf="@id/OpenAndCloseCamera" android:text="拍照" /> <TextView android:id="@+id/PhotographData" android:layout_width="150dp" android:layout_height="match_parent" android:layout_toRightOf="@+id/photograph" android:background="@drawable/solid_shape" android:gravity="center" android:ellipsize="end" android:singleLine="false" android:maxLines="3"/> <Button android:id="@+id/RecordingView" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:layout_toRightOf="@+id/PhotographData" android:text="录制视频" /> <TextView android:id="@+id/RecordingViewData" android:layout_width="150dp" android:layout_height="match_parent" android:layout_toRightOf="@+id/RecordingView" android:background="@drawable/solid_shape" android:gravity="center" android:ellipsize="end" android:singleLine="false" android:maxLines="3"/> <Button android:id="@+id/DeleteView" android:layout_width="150dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_margin="2dp" android:text="删除" /> </RelativeLayout> <LinearLayout android:id="@+id/ScreenTestContent" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/ScreenTest" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="屏幕测试" /> <Button android:id="@+id/ScreenCheck" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="屏幕检测" /> <TextView android:id="@+id/TouchScreenCheck" android:layout_width="150dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="触摸屏检测" /> <Button android:id="@+id/ScribingTest" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="划线测试" /> <Button android:id="@+id/SinglePointTest" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="单点测试" /> </LinearLayout> <LinearLayout android:id="@+id/NetPortContent" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/NetPort" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="网口" /> <TextView android:id="@+id/NetPortData" android:layout_width="150dp" android:layout_height="match_parent" android:ellipsize="end" android:layout_margin="2dp" android:gravity="center" android:background="@drawable/solid_shape"/> <TextView android:id="@+id/ping" android:layout_width="150dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="ping" /> <EditText android:id="@+id/pingInput" android:layout_width="150dp" android:layout_height="match_parent" android:hint="请输入" android:paddingLeft="10dp" android:text="192.168.1.1" android:layout_margin="2dp" android:inputType="text" android:maxLines="1"/> <Button android:id="@+id/pingSend" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="发送ping"/> <ScrollView android:id="@+id/scrollView" android:layout_width="200dp" android:layout_height="match_parent" android:fillViewport="true"> <TextView android:id="@+id/pingData" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/solid_shape" android:textSize="14sp" android:singleLine="false" android:maxLines="100" android:scrollbars="vertical" android:layout_margin="2dp"/> </ScrollView> <Button android:id="@+id/pingStop" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="停止"/> </LinearLayout> <RelativeLayout android:id="@+id/USBContent" android:layout_width="match_parent" android:layout_height="80dp"> <TextView android:id="@+id/USB" android:layout_width="160dp" android:layout_height="match_parent" android:background="@drawable/form_shape" android:gravity="center" android:text="USB接口" /> <TextView android:id="@+id/USBPath" android:layout_width="150dp" android:layout_height="match_parent" android:hint="未连接" android:ellipsize="end" android:layout_margin="2dp" android:gravity="center" android:background="@drawable/solid_shape" android:layout_toRightOf="@+id/USB"/> <Button android:id="@+id/getUSBPath" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="获取USB路径" android:layout_toRightOf="@+id/USBPath"/> <EditText android:id="@+id/WriteTextData" android:layout_width="150dp" android:layout_height="match_parent" android:inputType="text" android:paddingLeft="10dp" android:layout_toRightOf="@+id/getUSBPath"/> <Button android:id="@+id/WriteText" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="写入文本" android:layout_toRightOf="@+id/WriteTextData"/> <ScrollView android:id="@+id/scrollViewOpenTextData" android:layout_width="200dp" android:layout_height="match_parent" android:fillViewport="true" android:layout_toRightOf="@+id/WriteText"> <TextView android:id="@+id/OpenTextData" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/solid_shape" android:textSize="14sp" android:singleLine="false" android:maxLines="100" android:scrollbars="vertical" android:layout_margin="2dp" android:gravity="center"/> </ScrollView> <Button android:id="@+id/OpenText" android:layout_width="150dp" android:layout_height="match_parent" android:layout_margin="2dp" android:text="打开文本" android:layout_toRightOf="@+id/scrollViewOpenTextData"/> <Button android:id="@+id/DeleteText" android:layout_width="150dp" android:layout_height="match_parent" android:layout_alignParentRight="true" android:layout_margin="2dp" android:text="删除" /> </RelativeLayout> </LinearLayout> </ScrollView>使以上代码实现自适应屏幕大小,给出全部完整的代码
08-14
Duplicate id `@+id/nousbdriver` originally defined here Element androidx.constraintlayout.widget.ConstraintLayout is not closed :<!-- 保留 layout 标签但不声明 data --> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <com.kotei.sdk_module_uiskin.skin.view.SkinConstraintLayout android:id="@+id/update" android:layout_width="720dp" android:layout_height="386dp" android:layout_marginLeft="600dp" android:layout_marginTop="291dp" android:background="#FFFFFF" app:background4Skin="@color/white" app:background4Night="@drawable/rounded_bg_night"> <!--提示--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/tishi" android:layout_width="600dp" android:layout_height="56dp" android:layout_marginLeft="60dp" android:layout_marginTop="40dp" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--提示框--> <com.kotei.sdk_module_uiskin.skin.view.SkinTextView android:id="@+id/tishikuang" android:layout_width="600dp" android:layout_height="56dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="@color/text_pure" app:textColor4Skin="@color/text_pure" app:textColor4Night="@color/white" android:textSize="36sp" android:text="@string/update_tips_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" app:layout_constraintStart_toStartOf="@+id/tishi" app:layout_constraintTop_toTopOf="@+id/tishi" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--有数据可更新--> <com.kotei.sdk_module_uiskin.skin.view.SkinConstraintLayout android:id="@+id/youshujukegengxin" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="60dp" android:layout_marginTop="128dp" android:background="#FFFFFF" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--有数据可提示文言--> <com.kotei.sdk_module_uiskin.skin.view.SkinTextView android:id="@+id/youshujukegengxintext" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="@color/text_primary_day" app:textColor4Skin="@color/text_primary_day" app:textColor4Night="@color/text_primary_night" android:textSize="28sp" android:text="@string/update_updatable_data_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#FFFFFF" app:background4Skin="@drawable/rounded_bg" app:background4Night="@drawable/rounded_bg_night" app:layout_constraintStart_toStartOf="@+id/youshujukegengxin" app:layout_constraintTop_toTopOf="@+id/youshujukegengxin" /> </com.kotei.sdk_module_uiskin.skin.view.SkinConstraintLayout> <!--查看--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/chakan" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="60dp" android:layout_marginTop="272dp" android:background="#FFFF00" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--有数据可提示文言--> <TextView android:id="@+id/chakantext" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#FFFFFF" android:textSize="28sp" android:text="@string/update_watch_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#B87333" app:layout_constraintStart_toStartOf="@+id/chakan" app:layout_constraintTop_toTopOf="@+id/chakan" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--取消--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/quxiao" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="380dp" android:layout_marginTop="272dp" android:background="#FFFF00" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--有数据可提示文言--> <com.kotei.sdk_module_uiskin.skin.view.SkinTextView android:id="@+id/quxiaotext" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#FFFFFF" android:textSize="28sp" android:text="@string/update_cancel_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#818181" app:background4Skin="@color/color_i_see_day" app:background4Night="@color/blue_text_num" app:layout_constraintStart_toStartOf="@+id/quxiao" app:layout_constraintTop_toTopOf="@+id/quxiao" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--没有数据可更新--> <com.kotei.sdk_module_uiskin.skin.view.SkinConstraintLayout android:id="@+id/noupdate" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="60dp" android:layout_marginTop="128dp" android:background="#FFFFFF" app:background4Skin="@color/white" app:background4Night="@drawable/rounded_bg_night" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--没有数据可提示文言--> <com.kotei.sdk_module_uiskin.skin.view.SkinTextView android:id="@+id/noupdatetext" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="@color/text_primary_day" app:textColor4Skin="@color/text_primary_day" app:textColor4Night="@color/text_primary_night" android:textSize="28sp" android:text="@string/update_no_updatable_data_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#FFFFFF" app:background4Skin="@color/white" app:background4Night="@drawable/rounded_bg_night" app:layout_constraintStart_toStartOf="@+id/noupdate" app:layout_constraintTop_toTopOf="@+id/noupdate" /> </com.kotei.sdk_module_uiskin.skin.view.SkinConstraintLayout> <!--没有usb设备--> <com.kotei.sdk_module_uiskin.skin.view.SkinConstraintLayout android:id="@+id/nousbdriver" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="60dp" android:layout_marginTop="128dp" android:background="#FFFFFF" app:background4Skin="@color/white" app:background4Night="@drawable/rounded_bg_night" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--没有usb设备提示文言--> <TextView android:id="@+id/nousbdrivertext" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#99000000" android:textSize="28sp" android:text="@string/update_no_usb_driver_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#FFFFFF" app:layout_constraintStart_toStartOf="@+id/nousbdriver" app:layout_constraintTop_toTopOf="@+id/nousbdriver" /> </com.kotei.sdk_module_uiskin.skin.view.SkinConstraintLayout> <!--我知道了--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/iknow" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="220dp" android:layout_marginTop="272dp" android:background="#FFFF00" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--我知道了文言--> <com.kotei.sdk_module_uiskin.skin.view.SkinTextView android:id="@+id/iknowtext" android:layout_width="280dp" android:layout_height="64dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="#FFFFFF" android:textSize="28sp" android:text="@string/update_konwed_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" android:background="#818181" app:background4Skin="@color/color_i_see_day" app:background4Night="@color/plance_bg" app:layout_constraintStart_toStartOf="@+id/iknow" app:layout_constraintTop_toTopOf="@+id/iknow" /> </androidx.constraintlayout.widget.ConstraintLayout> <!--没有数据可更新--> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/nousbdriver" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="60dp" android:layout_marginTop="128dp" android:background="#FFFFFF" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--存储空间不足--> <com.kotei.sdk_module_uiskin.skin.view.SkinConstraintLayout android:id="@+id/nospace" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="60dp" android:layout_marginTop="128dp" android:background="#FFFFFF" app:background4Skin="@color/white" app:background4Night="@drawable/rounded_bg_night" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--有数据可提示文言--> <com.kotei.sdk_module_uiskin.skin.view.SkinTextView android:id="@+id/nospacetext" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="@color/text_primary_day" app:textColor4Skin="@color/text_primary_day" app:textColor4Night="@color/text_primary_night" android:textSize="28sp" android:text="@string/update_no_space_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" app:layout_constraintStart_toStartOf="@+id/nospace" app:layout_constraintTop_toTopOf="@+id/nospace" /> </com.kotei.sdk_module_uiskin.skin.view.SkinConstraintLayout> <!--是否结束数据更新--> <com.kotei.sdk_module_uiskin.skin.view.SkinConstraintLayout android:id="@+id/cancel" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="60dp" android:layout_marginTop="128dp" android:background="#FFFFFF" app:background4Skin="@color/white" app:background4Night="@drawable/rounded_bg_night" app:layout_constraintLeft_toLeftOf="@+id/update" app:layout_constraintTop_toTopOf="@+id/update"> <!--有数据可提示文言--> <com.kotei.sdk_module_uiskin.skin.view.SkinTextView android:id="@+id/canceltext" android:layout_width="600dp" android:layout_height="84dp" android:layout_marginLeft="0dp" android:layout_marginTop="0dp" android:gravity="center_horizontal|center_vertical" android:textColor="@color/text_primary_day" app:textColor4Skin="@color/text_primary_day" app:textColor4Night="@color/text_primary_night" android:textSize="28sp" android:text="@string/update_end_update_text" android:singleLine="false" android:maxLength="50" android:ellipsize="end" android:hint="@string/search_for_destination" app:layout_constraintStart_toStartOf="@+id/cancel" app:layout_constraintTop_toTopOf="@+id/cancel" /> </com.kotei.sdk_module_uiskin.skin.view.SkinConstraintLayout> </com.kotei.sdk_module_uiskin.skin.view.SkinConstraintLayout> </layout>
09-10
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值