zoukankan      html  css  js  c++  java
  • 1. 自定义视图属性

    res/values/attrs.xml   自定义属性值

    1 <?xml version="1.0" encoding="utf-8"?>
    2 <resources>
    3     <!-- 颜色属性值 -->
    4     <declare-styleable name="MyView">
    5         <attr name="rect_color" format="color"/>
    6     </declare-styleable>
    7 </resources>

    自定义控件  ---- 长方形

     1 import android.content.Context;
     2 import android.content.res.TypedArray;
     3 import android.util.AttributeSet;
     4 import android.view.View;
     5 
     6 public class MyRect extends View {
     7 
     8     /** 资源解析程序来使用的 */
     9     public MyRect(Context context, AttributeSet attrs) {
    10         super(context, attrs);
    11         
    12         // 设置颜色属性值
    13         TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.MyView);
    14         // 得到颜色值,指定一个默认值,红色。
    15         int color = ta.getColor(R.styleable.MyView_rect_color, 0xffff0000);
    16         // 设置颜色。
    17         setBackgroundColor(color);
    18         
    19         ta.recycle(); // 循环
    20     }
    21 
    22     public MyRect(Context context) {
    23         super(context);
    24     }
    25 
    26 }

    XML布局文件加载,自定义控件显示:

    1)加上,自定义命名空间,其中 com.jikexueyuan.myrect 是包名。

         xmlns:jkxy="http://schemas.android.com/apk/res/com.jikexueyuan.myrect"

    2)jkxy:rect_color="#ff00ff00"    才可正常使用。  

     1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:jkxy="http://schemas.android.com/apk/res/com.jikexueyuan.myrect"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:orientation="vertical"
     7     tools:context="com.jikexueyuan.myrect.MainActivity$PlaceholderFragment" >
     8 
     9     <com.jikexueyuan.myrect.MyRect
    10         android:layout_width="100dp"
    11         android:layout_height="100dp"
    12         jkxy:rect_color="#ff00ff00" />
    13 
    14 </LinearLayout>
    15 
    16 <!-- 
    17        1)加上,自定义命名空间,其中 com.jikexueyuan.myrect 是包名。
    18           xmlns:jkxy="http://schemas.android.com/apk/res/com.jikexueyuan.myrect"
    19        2)jkxy:rect_color="#ff00ff00"    才可正常使用。   
    20  -->
     1 import android.app.Activity;
     2 import android.app.Fragment;
     3 import android.os.Bundle;
     4 import android.view.LayoutInflater;
     5 import android.view.MenuItem;
     6 import android.view.View;
     7 import android.view.ViewGroup;
     8 
     9 public class MainActivity extends Activity {
    10 
    11     @Override
    12     protected void onCreate(Bundle savedInstanceState) {
    13         super.onCreate(savedInstanceState);
    14         setContentView(R.layout.activity_main);
    15 
    16         if (savedInstanceState == null) {
    17             getFragmentManager().beginTransaction()
    18                     .add(R.id.container, new PlaceholderFragment())
    19                     .commit();
    20         }
    21     }
    22 
    23     @Override
    24     public boolean onOptionsItemSelected(MenuItem item) {
    25         int id = item.getItemId();
    26         if (id == R.id.action_settings) {
    27             return true;
    28         }
    29         return super.onOptionsItemSelected(item);
    30     }
    31 
    32     /**
    33      * A placeholder fragment containing a simple view.
    34      */
    35     public static class PlaceholderFragment extends Fragment {
    36 
    37         public PlaceholderFragment() {
    38         }
    39 
    40         @Override
    41         public View onCreateView(LayoutInflater inflater, ViewGroup container,
    42                 Bundle savedInstanceState) {
    43             View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    44             return rootView;
    45         }
    46     }
    47 
    48 }
    1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2     xmlns:tools="http://schemas.android.com/tools"
    3     android:id="@+id/container"
    4     android:layout_width="match_parent"
    5     android:layout_height="match_parent"
    6     tools:context="com.jikexueyuan.myrect.MainActivity"
    7     tools:ignore="MergeRootFrame" />
  • 相关阅读:
    Vs2013在Linux开发中的应用(19): 启动gdb
    Codeforces Round #277 (Div. 2)---C. Palindrome Transformation (贪心)
    DataGridView依据下拉列表显示数据
    android POI搜索,附近搜索,周边搜索定位介绍
    HDU OJ Max sum 题目1003
    Android时时监測手机的旋转角度 依据旋转角度确定在什么角度载入竖屏布局 在什么时候载入横屏布局
    Hadoop架构设计、执行原理具体解释
    关联引用
    Linux性能诊断工具
    HDU 5089 Assignment(rmq+二分 或 单调队列)
  • 原文地址:https://www.cnblogs.com/androidsj/p/3945416.html
Copyright © 2011-2022 走看看