android安卓开发学习

2018-11-27  本文已影响0人  王乐城愚人云端

1.安装android studio

2.布局

同flex, div,table 等布局的概念
将res/layout/activity_main.xml视为html,

3.修改代码后立即热更新 ctrl + f10

4.LinearLayout(线性布局)

他的属性
android:layout_width="match_parent" // width: 100%
android:orientation="horizontal" // display: inline-block

4.TextView(文本视图)

android:layout_weight="2" // 权重占2
android:layout_width="wrap_content" // 内容的宽度
android:layout_height="fill_parent" // height: 100%
android:text="two" // 文字内容是two
android:background="#FFFF00" // 背景色

5.权重的特殊情况

有三个TextView
// 1
android:layout_weight="1"
android:layout_width="fill_parent"
// 2
android:layout_weight="2"
android:layout_width="fill_parent"
// 3
android:layout_weight="3"
android:layout_width="fill_parent"

结果是1占了2份,2点了1份,3没有了
原因:

每个元素都要求fill_parent 100%
但只有一个屏幕

元素个数count, 每个元素的权重weight, 总权重allWeight
每个元素所占宽度的公式:1-(1-weight)(weight/allWeight)

6.画横线

        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:background="#000000" />

7.RelativeLayout(相对布局)

给要定位的元素添加属性 // 基本上和position:fixed相同
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"

9.TableLayout(表格布局)

设置行和列,可设置隐藏,拉伸
下面属性是隐藏索引为0,2的列
android:collapseColumns="0,2"

拉伸的作用有点像flex中的作用

8.FrameLayout(帧布局)

感觉就是一个div

9.GridLayout(栅格布局)

感觉就是表格布局的升级版本
sdk4以上才有

10.TextView

id:为TextView设置一个组件id,根据id,我们可以在Java代码中通过findViewById()的方法获取到该对象,然后进行相关属性的设置,又或者使用RelativeLayout时,参考组件用的也是id!
layout_width:组件的宽度,一般写:wrap_content或者match_parent(fill_parent),前者是控件显示的内容多大,控件就多大,而后者会填满该控件所在的父容器;当然也可以设置成特定的大小,比如我这里为了显示效果,设置成了200dp。
layout_height:组件的宽度,内容同上。
gravity:设置控件中内容的对齐方向,TextView中是文字,ImageView中是图片等等。
text:设置显示的文本内容,一般我们是把字符串写到string.xml文件中,然后通过@String/xxx取得对应的字符串内容的,这里为了方便我直接就写到""里,不建议这样写!!!
textColor:设置字体颜色,同上,通过colors.xml资源来引用,别直接这样写!
textStyle:设置字体风格,三个可选值:normal(无效果),bold(加粗),italic(斜体)
textSize:字体大小,单位一般是用sp!
background:控件的背景颜色,可以理解为填充整个控件的颜色,可以是图片哦!
// 阴影
android:shadowColor:设置阴影颜色,需要与shadowRadius一起使用哦!
android:shadowRadius:设置阴影的模糊程度,设为0.1就变成字体颜色了,建议使用3.0
android:shadowDx:设置阴影在水平方向的偏移,就是水平方向阴影开始的横坐标位置
android:shadowDy:设置阴影在竖直方向的偏移,就是竖直方向阴影开始的纵坐标位置

// border 边框
先在res/drawable目录下创建border1.xml文件,并画出一个border背景图片

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- 实心 -->
    <solid android:color="@android:color/white" />
    <!-- 边框 -->
    <stroke
        android:width="1dp"
        android:color="@android:color/black" />
    <!-- 圆角 -->
    <corners android:radius="3dp" />
    <!-- 边距 -->
    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />
</shape>

再在文字属性中添加这个背景图片
android:background="@drawable/border1"

// 背景图片,设置大小,位置
在要java中才能设置

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        TextView textView = (TextView) findViewById(R.id.myTextView);
        Drawable drawable=getResources().getDrawable(R.drawable.yurencloud);
        drawable.setBounds(0,0,400,400);
        textView.setCompoundDrawables(null,null,drawable,null);
    }
}

其中myTextView是textView的id,要在
setContentView(R.layout.activity_main);
之后再引用

//设置超链接,在app中写html

        setContentView(R.layout.activity_main);
        TextView t1 = (TextView)findViewById(R.id.txtOne);
        String s1 = "<font color='blue'><b>百度一下,你就知道~:</b></font><br>";
        s1 += "<a href = 'http://www.baidu.com'>百度</a>";
        t1.setText(Html.fromHtml(s1));
        t1.setMovementMethod(LinkMovementMethod.getInstance());

11.自定义组件

1.先用java写好组件的各种属性
2.引用自定义组件,要带完整的包名

<com.example.asus.android1.EditTextWithDel
    android:layout_width="200dp"
    android:layout_height="wrap_content" />

12.adapter 适配器

我觉得这个就相当于一个数组,v-for,遍历的渲染的组件
比如最简单的遍历渲染一个字符串数组

   //要显示的数据
        String[] strs = {"基神","B神","翔神","曹神","J神"};
        //创建ArrayAdapter
        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (this,android.R.layout.simple_expandable_list_item_1,strs);
        //获取ListView对象,通过调用setAdapter方法为ListView设置Adapter设置适配器
        ListView list_test = (ListView) findViewById(R.id.list_test);
        list_test.setAdapter(adapter);

然后遍历输出到list_test这个ListView上

// 复杂一点的就是SimpleAdapter,可以自定义每项的样式,比如用一个xml来写好样式
// list_item

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <!-- 定义一个用于显示头像的ImageView -->
    <ImageView
        android:id="@+id/imgtou"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:baselineAlignBottom="true"
        android:paddingLeft="8dp" />

    <!-- 定义一个竖直方向的LinearLayout,把QQ呢称与说说的文本框设置出来 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    </LinearLayout>

</LinearLayout>

// 再在java中把数据遍历到每个list_item上

// 再把每个list_item输出到ListView上显示

上一篇下一篇

猜你喜欢

热点阅读