zoukankan      html  css  js  c++  java
  • andriod创建用户界面(1)

    一.UI知识Activity

    将UI放在一个Activity上面,可以在Activity的onCreate方法中添加UI。

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
    }

    如果想得到UI上的控件,可以通过findViewById方法查找,例如:

    ListView myListView = (ListView)findViewById(R.id.my_list_view);

    二、UI布局

    布局是对ViewGroup类的扩展,ViewGroup是对View的扩展。

    下面是常见的布局类:

    FrameLayout:通常是从左上角开始布局,不过如果多个子视图在同一个FrameLayout时,可能会出现重叠。

    LinearLayout:按水平或垂直对齐每个子视图。有些像StackPanel。

    RelativeLayout:定义子视图与其他视图或者屏幕边界的相对位置。

    GridLayout:行列布局,在4.0引入。

    2.1定义布局

    实例LinearLayout:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <fragment android:name="com.paad.todolist.NewItemFragment"
        android:id="@+id/NewItemFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
      />
      <fragment android:name="com.paad.todolist.ToDoListFragment"
        android:id="@+id/TodoListFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
      />
    </LinearLayout>

    match_parent:显示内容所需最小尺度,如字体的高度。

    wrap_content:使其充满,

    三.To-Do-List实例

    主要代码

    //找到ListView以便使用Adapter,

    //找到TextView以便监听其事件。

    ListView myListView = (ListView)findViewById(R.id.myListView);
        final EditText myEditText = (EditText)findViewById(R.id.myEditText);
       
        // Create the Array List of to do items
        final ArrayList<String> todoItems = new ArrayList<String>();
      
        // Create the Array Adapter to bind the array to the List View
        final ArrayAdapter<String> aa;
       //使用系统的list_item作为ArrayAdapter的item项
        aa = new ArrayAdapter<String>(this,
                                      android.R.layout.simple_list_item_1,
                                      todoItems);
      
        // Bind the Array Adapter to the List View
        myListView.setAdapter(aa);
     
        myEditText.setOnKeyListener(new View.OnKeyListener() {
          public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
              if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
                  (keyCode == KeyEvent.KEYCODE_ENTER)) {
                todoItems.add(0, myEditText.getText().toString());
                aa.notifyDataSetChanged();
                myEditText.setText("");
                return true;
              }
            return false;
          }
        });

    四、Fragment

    其灵活的特点,可以让Fragment达到复用,多个Activity使用同一个Fragment。同时一个Activity可以有多个Fragment组成。

    如果想在1.6以下版本,Activity必须继承FragmentActivity。

    1.Fragment的生命周期

    依赖于Activity。

    下面给出所有事件

    image

    onAttach()方法获取Activity,onCreate()初始化Fragment,onCreateView()设置Fragment的布局。当Activity和Fragment创建完了激发onActivityCreated()方法。onStart()方法生命周期算是正式开始,像小孩子长成大人了一样。onResume()当活动生命周期时触发,比如结婚的时间触发。onPause()方法活动生命周期结束时,像结婚后要度蜜月一样 。onSaveInstanceState()在活动结束后要记录一下状态。以便回来继续处理。

    onStop()方法,可见生命周期结束时调用。 onDestroyView()当Fragment的View分离时触发。onDestroy() 在生命周期结束时触发。onDetach是Fragment从Activity分离时发生。

    2.FragmentManager

    每个Activity都包括一个FragmentManager。用来访问Activity上面的Fragment,可以通过FragmentTransaction来添加替换。删除。

    3、FragmentTransaction可通过FragmentManager.BeginTransaction()获取。

    然后可以通过Transaction来将Fragment添加到Activity容器中。

    xml代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="horizontal"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      <FrameLayout
        android:id="@+id/ui_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
      />
      <FrameLayout
        android:id="@+id/details_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="3"
      />
    </LinearLayout>

    java代码:

    public class MyFragmentActivity extends Activity {

      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Inflate the layout containing the Fragment containers
        setContentView(R.layout.fragment_container_layout);
       
        FragmentManager fm = getFragmentManager();

        // Check to see if the Fragment back stack has been populated
        // If not, create and populate the layout.
        DetailsFragment detailsFragment =
          (DetailsFragment)fm.findFragmentById(R.id.details_container);
       
        if (detailsFragment == null) {
           FragmentTransaction ft = fm.beginTransaction();
           ft.add(R.id.details_container, new DetailsFragment());
           ft.add(R.id.ui_container, new MyListFragment());
           ft.commit();
         }
      }
    }

    注意以上划红线的部分是先在Activity中添加两个View——FrameLayout。后面通过代码把Fragment放入到View中的。

    4、查找Fragment

    FragmentManager manager=getFragmentManager();
    manager.findFragmentById(id);通常在有UI视图的情况适用
    manager.findFragmentByTag(string);,在没哟UI视图的情况适用。

    通常是把Fragment添加后有个id,然后才可以通过findFragmentById。
    manager.beginTransaction().add(fragment, tag);

    通常和后台Fragment使用。

    5.添加到BackStack,当按back键时。会回滚操作。

    image

    6.设置动画

        manager.beginTransaction().setCustomAnimations(enter, exit)();

    7.Fragment和Activity之间的接口

    可以通过Fragment的onAttach()来获得Activity的引用。也可以getActivity() 来获取Activity。

    通常获得Activity是为了让Fragment属性变化时,来调用Activity的一些方法。所以可以在Fragment中定义指定的接口,然后让Activity来调用接口即可。

    ListFragment的例子:

    public class ToDoListFragment extends ListFragment {
      
    }

    FragmentManager fm = getFragmentManager();
    ToDoListFragment todoListFragment =
      (ToDoListFragment)fm.findFragmentById(R.id.TodoListFragment);

    // Create the array list of to do items
    todoItems = new ArrayList<String>();
     
    // Create the array adapter to bind the array to the listview
    aa = new ArrayAdapter<String>(this,
                                  android.R.layout.simple_list_item_1,
                                  todoItems);
     
    // Bind the array adapter to the listview.
    todoListFragment.setListAdapter(aa);

  • 相关阅读:
    httpd-2.2 配置及用法完全攻略
    在 Ubuntu 16.04 上安装 LEMP 环境之图文向导
    Zookeeper集群搭建
    smem – Linux 内存监视软件
    X.Org可能将失去它的域名x.org
    Docker 容器测试全探索
    Unix操作系统中UUCP知识详细讲解
    常用的Git Tips
    Unix操作系统中UUCP知识详细讲解
    将博客搬至CSDN
  • 原文地址:https://www.cnblogs.com/lzhp/p/3805284.html
Copyright © 2011-2022 走看看