zoukankan      html  css  js  c++  java
  • 安卓笔记之配置第一个程序

    配置一个简单的程序

    主要通过src

                                |-java

    和       res

                                |-layout

                                    |-main.xml

    以及        |-values

                                         |-strings.xml   来完成

    第一个在strings.xml没有参与的情况下通过ID搭配配置文件

             在main.xml下配置textview和button

                               <TextView

            android:id="@+id/mytext"     -->要调取的ID

            android:layout_width="fill_parent"  ----》宽度

            android:layout_height="wrap_content" ----》高度

           />

        <Button

            android:id="@+id/mybut"    -->按钮ID

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

       />

    然后在java程序中来调取

        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

            TextView text = (TextView) super.findViewById(R.id.mytext);//取得TextView组件

            text.setText(“中国人”);//设置显示文字

            Button but= (Button) super.findViewById(R.id.mybut);//取得按钮组件

            but.setText(“按我”);

        }

    第二种在strings参与的情况下

        一时strings参与xml

        <TextView

            android:id="@+id/mytext"     -->要调取的ID

            android:layout_width="fill_parent"  ----》宽度

            android:layout_height="wrap_content" ----》高度

            android:text="@strings/text" -->要展示的文本 />

        <Button

            android:id="@+id/mybut"    -->按钮ID

            android:layout_width="fill_parent"

            android:layout_height="wrap_content"

            android:text="@strings/msg" />

        public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

            //TextView text = (TextView) super.findViewById(R.id.mytext);//取得TextView组件

            //text.setText(“中国人”);//设置显示文字

            //Button but= (Button) super.findViewById(R.id.mybut);//取得按钮组件

            //but.setText(“按我”);

        }

        二是strings参与java

        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.main);

            TextView text = (TextView) super.findViewById(R.id.mytext);//取得TextView组件

            text.setText(R.string.text);//设置显示文字

            Button but= (Button) super.findViewById(R.id.mybut);//取得按钮组件

            but.setText(R.string.msg);

        }

    这两种情况都要在strings中添加

    <resources>

     

        <string name="hello">Hello World, ZhActivity!</string>

        <string name="app_name">Zh</string>

        <string name="text">中国人</string>

        <string name="msg">按我</string>

     

    </resources>

    第三种情况

    通过程序动态生成

      public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            LinearLayout layout = new LinearLayout(this);

            TextView text = new TextView(this);

            text.setText(super.getString(R.string.text));//通过strings.xml文件设置文字

            Button but = new Button(this);//定义按钮

            but.setText(super.getString(R.string.msg));//配置组件文字

            layout.addView(text);//向布局管理器之中增加文本组件

            layout.addView(but);//想布局管理器之中增加按钮组件

            super.setContentView(layout);//设置要使用的布局管理器

        }

  • 相关阅读:
    zookeeper和Eureka对CAP理论的支持
    redis缓存穿透、缓存击穿、缓存雪崩
    CAP原则和BASE理论
    bin log、redo log、undo log和MVVC
    在ROS中使用Python3
    Windows下使用AutoSSH,并作为服务自启动(不用安装Cygwin)
    回收站的正确使用方法
    Windows下好用到必须开机自启的小工具
    PreferenceScreen监听子项的刷新
    安卓普通类通过classloader访问资源文件
  • 原文地址:https://www.cnblogs.com/kakaxi/p/2243096.html
Copyright © 2011-2022 走看看