zoukankan      html  css  js  c++  java
  • LayoutParams布局

    AbsoluteLayout.LayoutParams可以重新设置坐标,然后调用setLayoutParams
    LinearLayout.LayoutParams可以调用setMargins();来移动控件位置
    比如在调用rootLayout.addView(view2);后面再加上
            LinearLayout.LayoutParams param3 = new LinearLayout.LayoutParams(50, 50);
            param3.setMargins(-200, 300, 0, 0);
            view2.setLayoutParams(param3);

    ///////////////////////

    view = new FrameLayout(activity);
      view.setOnTouchListener(otListener);
      
      // keyboard
      LinearLayout keyboard = new LinearLayout(activity);
      keyboard.setBackgroundResource(R.drawable.skb_back);
      keyboard.setOrientation(LinearLayout.VERTICAL);
      keyboard.setPadding(0, 4, 0, 6);
      FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
        LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
      lp.gravity = Gravity.BOTTOM;
      keyboard.setLayoutParams(lp);
      view.addView(keyboard);
      
      tvKeys = new TextView[4][];
      tvKeys[0] = new TextView[10];
      tvKeys[1] = new TextView[9];
      tvKeys[2] = new TextView[9];
      tvKeys[3] = new TextView[5];
      LinearLayout.LayoutParams lineLp = new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT, keyHeight);
      lineLp.setMargins(0, 9, 0, 8);
      for (int i = 0; i < tvKeys.length; i++) {
       LinearLayout llLine = new LinearLayout(activity);
       llLine.setLayoutParams(lineLp);
       for (int j = 0; j < tvKeys[i].length; j++) {
        LinearLayout.LayoutParams keyLp = new LinearLayout.LayoutParams(
          keyWidth, LayoutParams.FILL_PARENT);
        keyLp.setMargins(4, 0, 4, 0);
        tvKeys[i][j] = new TextView(activity);
        tvKeys[i][j].setBackgroundResource(R.drawable.skey_normal_back);
        tvKeys[i][j].setGravity(Gravity.CENTER);
        tvKeys[i][j].setText(keys[state][i][j]);
        tvKeys[i][j].setTextColor(0xffffffff);
        tvKeys[i][j].setTextSize(20);
        tvKeys[i][j].setLayoutParams(keyLp);
        llLine.addView(tvKeys[i][j]);
       }
       keyboard.addView(llLine);
      }

    Android笔记之一:adapter

        

    先从ListAdapter说起吧。什么是ListAdapter?

    ListAdapter继承于Adapter,它是ListView和其里边数据的适配器。也就是要让一个ListView现实出来。为此需要3个东西。

    ListView(需要被显示的列表)

    Data,和ListView绑定的数据,一般是一个Cursor或者一个字符串数组。

    ListAdapter,是data和ListView的桥梁,其一个适配作用。

    Adapter 的数据源有三种

    ArrayAdapter

    ListAdapter

    SimpleCursorAdapter

    自己实现的CustomAdapter

    -------------------------------

    android adapter的体系文章分类:Java编程

    在android开发中列表的使用是十分常见的。google对列表的封装使列表既有显示传统文本列表的能力,也有加入了诸如选择项、复选项等处理事件的能力。这里写一些我这几天对这个问题的理解。在android的api中,LIST和adapter都被放在了android.widget包内。包内的具体结构我这里先不展示了,主要侧重列表和 adapter。adapter的作用就是将要在列表内显示的数据和列表本身结合起来。列表本身只完成显示的作用,其实他就是继承自VIEWGROUP 类。但是他又有一个独特的函数就是setAdapter()就是完成了view和adapter的结合。adapter如同其本身含义,其实就是一个适配器,他可以对要显示的数据进行统一的封装,主要是将数据变成view提供给list。我们先来看看adapter的体系:public interface Adapter----0层(表示继承体系中的层次)public interface ExpandableListAdapter---(无所谓层次因为没有其他接口继承实现它)这是adapter的始祖,其他个性化的adapter均实现它并加入自己的接口。public interface ListAdapter----1层public interface SpinnerAdapter----1层public interface WrapperListAdapter----2层(实现ListAdapter)以上接口层面上的体系已经完了。可以看出来作为widgetview的桥梁adapter其实只分为2种:ListAdapter和 SpinnerAdapter以及ExpandableListAdapter。也就是说所有widget也就是基于list和spinne与 ExpandableList三种view形式的。由于在实际使用时,我们需要将数据加入到Adapter,而以接口形式呈现的adapter无法保存数据,于是Adapter就转型为类的模式。public abstract class BaseAdapter----2层(实现了ListAdapter和SpinnerAdapter)以抽象类的形式出现构造了类型态下的顶层抽象,包容了List和Spinnerpublic class ArrayAdapter----3层public class SimpleAdapter---3层public class CursorAdapter----3层(CursorAdapter其后还有子类这里先不探讨)基本体系有了之后,让我们看看顶层Adapter里有哪些方法(只列举常用的):abstract Object getItem_r(int position)abstract int getCount_r()abstract long getItemId_r(int position)abstract int getItemViewType_r(int position)abstract View getView_r(int position,View convertVeiw,ViewGroup parent)以上是比较重要的方法,ArrayAdapter他们也是重新实现以上方法的。在实际的开发过程中,往往我们要自己做属于自己的Adapter,以上方法都是需要重新实现的。这个在android提供的APIdemo例子中可以看到。

    -----------------------------

    (转)android adapter的学习

    我在刚玩android 时候,对这个adapter很不理解,到底是什么原理呢? 适配器,哎,只知道setAdapter()把参数传进去,系统就显示出来了。今天,针对这个东西,我们做个系统详细的分析.listview加载adapter过程是这样的.1 先判断adapter 有多少数据项,根据这个数据确定有多少item.2 确定每个item里加载哪个View. 3 把View里加载要显示的数据.问提一个一个来解决. 第一个问题: 因为adapter都要关联一个list .有来存储数据.list的项数就是Item的

    数目. 我们在重载BaseAdapter 时候,都要实现这个函数

    public int getCount() {return weatherList.size();}哎,这个函数就是确定关联条目的.第二个问题 哪来的view 呢, 当然我们自己创建的.重载BaseAdapter时候你要实现getView()这个函数,就

    是这个view.第三个问题,你自己创建的view.加载哪些数据你该知道的.呵呵.张豪就喜欢看例子,这个小伙子技术,管理都很牛,得以他为榜样. 得努力.public class CustomAdapterActivity extends ListActivity{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);ArrayListWeather weatherList = new ArrayListWeather();Weather w = new Weather( "London", 17, Weather.OVERCAST );weatherList.add( w );w = new Weather( "Paris", 22, Weather.OVERCAST );weatherList.add( w );w = new Weather( "Athens", 29, Weather.SUNNY );weatherList.add( w );w = new Weather( "Stockholm", 12, Weather.RAIN );weatherList.add( w );WeatherAdapter weatherAdapter = new WeatherAdapter(this,weatherList );setListAdapter( weatherAdapter );}}哎,这个大家都很清楚,关键问题是weatherAdapter 哪来的呢? 自己创建的啊,如果创建呢?public class WeatherAdapter extends BaseAdapter {private Context context;private ListWeather weatherList;  这就是adapter关联的List,用来存储数据.还记的ArrayList 要往

    里传参数吗? 传的也是这个类型啊.呵呵public WeatherAdapter(Context context, ListWeather weatherList ) {this.context = context;this.weatherList = weatherList;}public int getCount() {return weatherList.size();}public Object getItem(int position) {return weatherList.get(position);}public long getItemId(int position) {return position;}public View getView(int position, View convertView, ViewGroup parent) {Weather weather = weatherList.get(position);return new WeatherAdapterView(this.context, weather );}}哎,这段告诉了我们,有多少个Item, 可以通过getCount()得到了。 可是View 哪来的呢?当然是getView ()这个函数提供.这个view 的获取就多中多样了,我们可以传个LayoutID. 通过Inflater出来,也可以自己创建个,只要出来就行.

    在这里,我们自己创建个View. 这个View.是个VIewGroup.class WeatherAdapterView extends LinearLayout {public static final String LOG_TAG = "WeatherAdapterView";public WeatherAdapterView(Context context,Weather weather ) {super( context );this.setOrientation(HORIZONTAL);LinearLayout.LayoutParams cityParams =new LinearLayout.LayoutParams(100, LayoutParams.WRAP_CONTENT);cityParams.setMargins(1, 1, 1, 1);TextView cityControl = new TextView( context );cityControl.setText( weather.getCity() );addView( cityControl, cityParams);LinearLayout.LayoutParams temperatureParams =new LinearLayout.LayoutParams(20, LayoutParams.WRAP_CONTENT);temperatureParams.setMargins(1, 1, 1, 1);TextView temperatureControl = new TextView(context);temperatureControl.setText( Integer.toString( weather.temperature ) );addView( temperatureControl, temperatureParams);LinearLayout.LayoutParams skyParams =new LinearLayout.LayoutParams(25, LayoutParams.WRAP_CONTENT);ImageView skyControl = new ImageView( context );Log.d( LOG_TAG, weather.getCity()+" - "+weather.sky );skyControl.setImageResource( weather.getSkyResource() );addView( skyControl, skyParams );}}有了这个就很清楚了. 呵呵,很明白吧, 一定得深入,细致的理解.

  • 相关阅读:
    python批量裁剪图片
    Theano 报错:No suitable SharedVariable constructor could be found. Are you sure all kwargs are supported? We do not support the parameter dtype or type
    清华镜像连接
    ubuntu16.04查看占用GPU的程序
    pycharm报错:ImportError: libcusolver.so.8.0: cannot open shared object file: No such file or directory
    PyMysql的基本操作
    关于爬虫解析页面时的一些有意思的坑
    关于爬虫解析页面时的一些有意思的坑
    python 的一些高级函数
    python 的一些高级函数
  • 原文地址:https://www.cnblogs.com/youlechang123/p/5716922.html
Copyright © 2011-2022 走看看