Android笔记--属性

我们在布局文件中,会对一些控件设置一些属性,这些属性都是定义好的,使用的时候设定该属性的,否则就是用默认值。主要看framework下是如何定义属性的及使用。

一、属性定义,在values中创建一个attrs.xml文件,然后如下几种格式定义属性、

1、一般格式

     <attr name = "textColor" format = "color" />  

其中Format值有如下几种

 "reference" //引用  
 "color" //颜色  
 "boolean" //布尔值  
 "dimension" //尺寸值  
 "float" //浮点值  
 "integer" //整型值  
 "string" //字符串  
 "fraction" //百分数,比如200% 

属性定义可以指定多种类型,如

  <attrname="textColor" format="reference|color" />

2、枚举类型格式

          <attrname="streamType">
              <enum name="voice"value="0" />
              <enum name="system"value="1" />
              <enum name="ring"value="2" />
              <enum name="music"value="3" />
              <enum name="alarm"value="4" />
          </attr>

3、标志位格式

      <attr name="textStyle">
          <flag name="normal"value="0" />
          <flag name="bold"value="1" />
          <flag name="italic"value="2" />
      </attr>

二、在定义的组件的构造函数中进行设置

      public CheckBoxPreference(Context context,AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
          
          TypedArray a =context.obtainStyledAttributes(attrs,
                  com.android.internal.R.styleable.CheckBoxPreference,defStyle, 0);
          setSummaryOn(a.getString(com.android.internal.R.styleable.CheckBoxPreference_summaryOn));
          setSummaryOff(a.getString(com.android.internal.R.styleable.CheckBoxPreference_summaryOff));
          setDisableDependentsState(a.getBoolean(
                  com.android.internal.R.styleable.CheckBoxPreference_disableDependentsState,false));
          a.recycle();
      }

三、XML文件中使用

定义xmlns:android=http://schemas.android.com/apk/res/android,这是一个XML命名空间,告诉Android开发工具你准备使用Android命名空间里的一些通用属性。

在所有AndroidXML设计文件中最外层的标记必须使用这个树形。有了它,你就可以alt+/作为提示,有哪些字段可以使用。

然后可以设置各个属性,例如:

  <VolumePreference
              android:key="volume_setting"
              android:title="@string/alarm_volume_title"
              android:dialogTitle="@string/alarm_volume_title"
              android:streamType="alarm"/>

四、自定义属性

在values目录下创建attrs.xml文件

  <?xmlversion="1.0" encoding="utf-8"?>
  <resources>
      <declare-styleablename="CustomView">      
          <attr name="textColor"format="color" />      
          <attr name="textSize"format="dimension" />      
      </declare-styleable>
  </resources>

创建自定义view,在构造函数中对属性进行设置

      public CustomView(Context context, AttributeSet attrs) { 
      super(context, attrs); 
      mPaint = new Paint(); 
      TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.CustomView); 
      int textColor = a.getColor(R.styleable.CustomView_textColor,0xFFFFFF00); 
      float textSize = a.getDimension(R.styleable.CustomView_textSize,20); 
      mPaint.setTextSize(textSize); 
      mPaint.setColor(textColor); 
      a.recycle(); 
      }

在布局文件中使用属性,如果没有设定就是用默认的

  <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:test="http://schemas.android.com/apk/res/com.example.customattr"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
   
      <com.example.customattr.CustomView
           android:layout_width="fill_parent"  
           android:layout_height="fill_parent"
           test:textSize="50px"
           test:textColor="#000000"/>
   
  </LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于Android登录后台实现APP登录功能,你需要进行以下步骤: 1.在Android项目中添加网络权限 在AndroidManifest.xml文件中添加以下代码: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 2.使用HttpURLConnection发送POST请求 可以使用HttpURLConnection类来发送POST请求,以下是一个示例代码: ```java URL url = new URL("http://yourbackend.com/login"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); //添加请求头 conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); //添加请求体 JSONObject jsonParam = new JSONObject(); jsonParam.put("username", "yourusername"); jsonParam.put("password", "yourpassword"); OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); writer.write(jsonParam.toString()); writer.flush(); //读取响应 BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuffer sb = new StringBuffer(); String line; while ((line = reader.readLine()) != null) { sb.append(line); } reader.close(); String response = sb.toString(); ``` 3.解析后台返回的数据 在上面的代码中,我们使用了JSONObject类来创建一个JSON请求体。在得到后台返回的数据后,你需要使用相应的JSON解析器来解析返回的数据。例如,可以使用GSON库来解析JSON数据: ```java Gson gson = new Gson(); LoginResponse response = gson.fromJson(sb.toString(), LoginResponse.class); ``` 其中,LoginResponse是一个你自己定义的Java类,用来表示后台返回的数据。你需要根据后台返回的数据来定义这个类的属性。 以上就是实现Android登录后台的基本步骤,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值