Android五种布局方式——LinearLayout、RelativeLayout 、TableLayout....
Android使用XML声明界面布局
将程序的表现层和控制层分离修改用户界面时,无需更改程序的源代码
可视化工具设计用户界面
LinearLayout线性布局
AbsoluteLayout坐标布局
RelativeLayout相对布局
FrameLayout帧布局
TableLayout表格布局
GridLayout
1.LinearLayout线性布局
2.FrameLayout帧布局
最简单的布局形式
组件都放在屏幕的左上角,组件是按次序加入次序层叠在一起,上一层的控件会覆盖下一层的控件
3.TableLayout表格布局
Tablelayout以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件。
当为TableRow对象时,可在TableRow下添加子控件,默认情况下,每个子控件占据一列。有多少个子控件就有多少列
当为View时,该View将独占一行
4.RelativeLayout相对布局
一种非常灵活的布局方式
通过指定界面元素与其他元素的相对位置关系,确定界面中所有元素的布局位置
特点:能够最大程度保证在各种屏幕类型的手机上正确显示界面布局
布局范例
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jreduch7292.BuJuActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="热门评论"
android:textColor="#f40505"
android:layout_marginLeft="80dp"
android:textSize="30dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@mipmap/zyf"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HighSmile山人"
android:textColor="#1048ef"
android:textSize="30dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="丹麦治安一直都很好,这种事估计可以震动他们全国了"
android:textColor="#000000"
android:textSize="30dp"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:orientation="horizontal"
android:layout_marginLeft="80dp"
android:background="#8c8282"
>
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="@mipmap/zyfzyf"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="丹麦连发2起枪击案致1死6伤尚不清楚是否相关"
android:textColor="#000000"
android:textSize="25dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="80dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5小时前"
android:textColor="#000000"
android:textSize="20dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/like"
android:layout_marginLeft="50dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="200"
android:textColor="#000000"
android:textSize="20dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/message"
android:layout_marginLeft="30dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1"
android:textColor="#000000"
android:textSize="20dp"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="查看1条评论"
android:textColor="#1295f2"
android:textSize="25dp"
android:layout_marginLeft="80dp"
/>
</LinearLayout>
</RelativeLayout>
高级控件图片左右划屏
<pre name="code" class="java">package com.example.jreduch7292;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
import uk.co.senab.photoview.PhotoView;
public class ViewPagerActivity extends AppCompatActivity {
private ViewPager vp;
private List<PhotoView> myData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager);
vp= (ViewPager) findViewById(R.id.vp);
myData=new ArrayList<>();
PhotoView img=new PhotoView(this);
img.setImageResource(R.mipmap.zyfzyf);
myData.add(img);
img=new PhotoView(this);
img.setImageResource(R.mipmap.zyf);
myData.add(img);
img=new PhotoView(this);
img.setImageResource(R.mipmap.zz);
myData.add(img);
img=new PhotoView(this);
img.setImageResource(R.mipmap.zzz);
myData.add(img);
vp.setAdapter(new MyViewPagerAdapter(myData));
}
public class MyViewPagerAdapter extends PagerAdapter{
private List<PhotoView> myData;
public MyViewPagerAdapter(List<PhotoView> myData){
this.myData=myData;
}
@Override
public int getCount() {
return myData.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
container.addView(myData.get(position));
return myData.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(myData.get(position));
}
}
}
作者:冲天之峰 20160729