Andorid layout

Android Studio中创建布局文件有两种方式:

在XML文本编辑器中直接编写代码和使用可视化布局编辑器通过拖拽控件自动生成。

可视化布局编辑器可以通过拖拽控件生成布局,可以实时预览布局的效果。可以在不安装APP的情况下,随时查看布局在不同分辨率、不同版本、不同品牌手机上的显示效果, 节省了大量调试界面的时间。

在打开 layout xml文件时,会出现Design (可视化编辑器)和Text(对应xml文件)两个TAB;

怎样生成layout文件

直接命名个xml文件是不行的,可以右键 New -> Layout resource file

Layout的类型

A ViewGroup is a special view that can contain other views (called children.) The view group is the base class for layouts and views containers. This class also defines the ViewGroup.

Android contains the following commonly used ViewGroupsubclasses:

 LinearLayout

A layout that arranges other views either horizontally in a single column or vertically in a single row.

LinearLayout(horizontal/vetical) 是个大小可以调节的区域,其中的view 元素是水平或者垂直放置的(并不是随意拖动位置)

FrameLayout

  • LinearLayout
  • ConstraintLayout
  • FrameLayout
  • ScrollView

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that’s scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

Child views are drawn in a stack, with the most recently added child on top.

ScrollView

A view group that allows the view hierarchy placed within it to be scrolled. Scroll view may have only one direct child placed within it. To add multiple views within the scroll view, make the direct child you add a view group, for example LinearLayout, and place additional views within that LinearLayout.

ContraintLayout(主要方法)

ConstraintLayout完全解析

http://blog.csdn.net/guolin_blog/article/details/53122387

View元素

A View occupies a rectangular area on the screen and is responsible for drawing and event handling.The View class is a superclass for all GUI components in Android.

Commonly used Views are :

  • EditText
  • ImageView
  • TextView
  • Button
  • ImageButton
  • CheckBox

Attributes

id : These ids are typically assigned in the layout XML files, and are used to find specific views within the view tree.View IDs need not be unique throughout the tree, but it is good practice to ensure that they are at least unique within the part of the tree you are searching.

Syntax : android:id=”@+id/my_id”

height & width : As the name itself suggests, both of these attributes describes the height and width of the given view. These are necessary attributes for every view in a xml file.

Syntax : android:layout_width="match_parent"
 android:layout_height="match_parent"

FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which means that the view wants to be as big as its parent (minus padding)WRAP_CONTENT, which means that the view wants to be just big enough to enclose its content (plus padding). Ideally, Value should be in “dp” i.e density independent pixels.

Padding & margin

Padding is the space inside the border, between the border and the actual view’s content. Note that padding goes completely around the content.

Margins are the spaces outside the border, between the border and the other elements next to this view. In the image, the margin is the grey area outside the entire object.

Syntax : android:padding="10dp" android:layout_margin="10dp"

Gravity & Layout_gravity

android:gravity sets the gravity of the content of the View its used on.
android:layout_gravity sets the gravity of the View or Layout in its parent.

怎样使用资源文件

比如layout文件:activity_main.xml等生成

* This class was automatically generated by the* aapt tool from the resource data it found.  It
* should not be modified by hand.
public final class R {},代码就是通过这个类和资源文件关联的。

xml文件:tools:context="io.github.introml.activityrecognition.MainActivity">和使用context关联

Android程序员都知道Activity调用setContentView的方法是将xml布局文件加载到Activity中

https://www.jianshu.com/p/9acdf27aae06 setContentView背后的故事

调用setContentView(R.layout.activity_camera[layout资源文件的名字]), 然后通过R.id.xxxx找到对应的View.

private TextView downstairsTextView; 
downstairsTextView = (TextView) findViewById(R.id.downstairs_prob);
通过资源Id找到对应的View
<TextView
    android:id="@+id/downstairs_prob"
    android:layout_weight="1"
    android:textAlignment="center"
    android:textSize="18sp" />
downstairsTextView.setText(Float.toString(round(results[0], 2)));
浮点数保留小数点后两位,并转化伟字符串显示
private static float round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.floatValue();
}

 

猜你喜欢

转载自blog.csdn.net/u011279649/article/details/84068022