自定义XML

作者: 慢慢来111 | 来源:发表于2016-11-26 10:16 被阅读26次

跑马灯 自定义XML

跑马灯

  • 跑马灯的实现方式有两种,这两种方式文字必须超过屏幕的长度
    • 第一种:
    android:focusable-true
    
    • 第二种
    创建一个类,继承于TextView,实现系统的构造方法,再实现一个
     @Override
     @ExportedProperty(category = "focus")
     public boolean isFocused() {
         // TODO Auto-generated method stub
         return true;
     }
     不管实际上textView有没有焦点,我们强制返回true
    

布局

  • 一个继承于Activity的类,的xml文件,创建一个
    <GridView 
        android:id "@+id/gv"
        android:layout_width "match_parent"
        android:layout_height "0dp"
        android:layout_weight "1"
        android:verticalSpacing "20dp"
        android:numColumns "3"
        ></GridView>
  • GridView需要一个Adapter,创建一个继承于BaseAdapter的类
    class HomeAdapter extends BaseAdapter{
        private String[] mItems =new String[]{
                "手机防盗",
                "通讯卫士",
                "软件管理",
                "进程管理",
                "流量统计",
                "手机杀毒",
                "缓存清理",
                "高级工具",
                "设置中心"
        };
        private int[] mImages = new int[]{
                R.drawable.home_safe,
                R.drawable.home_callmsgsafe,
                R.drawable.home_apps,
                R.drawable.home_taskmanager,
                R.drawable.home_netmanager,
                R.drawable.home_trojan,
                R.drawable.home_sysoptimize,
                R.drawable.home_tools,
                R.drawable.home_settings
        };
        @Override
        public int getCount() {
            return mItems.length;
        }

        @Override
        public Object getItem(int arg0) {
            return mItems[arg0];
        }
        @Override
        public long getItemId(int arg0) {
            return arg0;
        }
        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            View view = View.inflate(ShouYeActivity.this, R.layout.home_list_item, null);
            ImageView ivItem = (ImageView) view.findViewById(R.id.imageView1);
            TextView textView = (TextView) view.findViewById(R.id.textView1);
            textView.setText(mItems[arg0]);
            ivItem.setImageResource(mImages[arg0]);
            return view;
        }
    }
  • Adapter中的布局文件XML--home_list_item,这个XML文件定义的是GridView中一个item的样式。
<LinearLayout xmlns:android "http://schemas.android.com/apk/res/android"
    android:layout_width "wrap_content"
    android:layout_height "wrap_content"
    android:gravity "center"
    android:orientation "vertical" >

    <ImageView
        android:id "@+id/imageView1"
        android:layout_width "wrap_content"
        android:layout_height "wrap_content"
        android:src "@drawable/home_apps" />

    <TextView
        android:id "@+id/textView1"
        android:layout_width "wrap_content"
        android:layout_marginTop "5dp"
        android:textColor "#000"
        android:textSize "16sp"
        android:layout_height "wrap_content"
        android:text "手机防盗" />
</LinearLayout>

示例

示例图片1
示例图片2

  • 自定义XML文件 view_setting_item.xml
    <TextView
        android:id "@+id/tv_title"
        android:layout_width "wrap_content"
        android:layout_height "wrap_content"
        android:textColor "#000"
        android:textSize "22sp" />

    <TextView
        android:id "@+id/tv_desc"
        android:layout_width "wrap_content"
        android:layout_height "wrap_content"
        android:layout_below "@id/tv_title"
        android:layout_marginTop "3dp"
        android:textColor "#a000"
        android:textSize "18sp" />

    <CheckBox
        android:id "@+id/cb"
        android:clickable "false"
        android:focusable "false"
        android:layout_width "wrap_content"
        android:layout_height "wrap_content"
        android:layout_alignParentRight "true"
        android:layout_centerVertical "true" />

    <View
        android:layout_width "match_parent"
        android:layout_height "0.2dp"
        android:layout_alignParentBottom "true"
        android:background "#a000" />

</RelativeLayout>
  • 自定义一个RelativeLayout的类SettingItemView,并且实现构造方法
public class SettingItemView extends RelativeLayout{

    private TextView de;
    private CheckBox cb;
    private TextView textView;
    public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    public SettingItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public SettingItemView(Context context) {
        super(context);
        initView();
    }
    //初始化布局
    //获得上下文对象getContext()
    private void initView(){
        //将自定义好的布局文件设置给当前的settingItemView
        View view = View.inflate(getContext(), R.layout.view_setting_item, this);
        textView = (TextView) findViewById(R.id.tv_title);
        de = (TextView) findViewById(R.id.tv_desc);
        cb = (CheckBox) findViewById(R.id.cb);
    }

    //构造一个设置view上的textView文字的方法
    public void setTitle(String title) {
        textView.setText(title);
    }
    //构造一个设置view上的textView文字的方法
    public void setDe(String desc) {
        de.setText(desc);
    }
    //构造一个查看chcekBox是否是选择的状态。
    public boolean isChecked() {
        
        return cb.isChecked();
    }
    //构造一个设置chcekBox选择状态的方法
    public void setChecked(boolean check) {
        cb.setChecked(check);
    }
}
  • 在Activity的XML文件中(setting),用上面的SettingItemView创建
<?xml version "1.0" encoding "utf-8"?>
<LinearLayout xmlns:android "http://schemas.android.com/apk/res/android"
    android:layout_width "match_parent"
    android:layout_height "match_parent"
    android:orientation "vertical" >

    <TextView
        style "@style/TitleStyle"
        android:text "设置中心" />

    <com.example.kongjian.SettingItemView
        android:id "@+id/siv_update"
        android:layout_width "match_parent"
        android:layout_height "wrap_content" />

</LinearLayout>
  • 在Activity中
public class SettingActivity extends Activity{

    private SettingItemView sivUpdate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting);
        
        sivUpdate = (SettingItemView) findViewById(R.id.siv_update);
        sivUpdate.setTitle("自动更新设置");
        sivUpdate.setDe("自动更新已开启");
        sivUpdate.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                //判断当前的勾选状态
                if (sivUpdate.isChecked()) {
                    //设置不勾选
                    sivUpdate.setChecked(false);
                    sivUpdate.setDe("自动更新已关闭");
                }else {
                    sivUpdate.setChecked(true);
                    sivUpdate.setDe("自动更新开启");
                }
            }
        });
    }
}

相关文章

网友评论

    本文标题:自定义XML

    本文链接:https://www.haomeiwen.com/subject/zrawpttx.html