zoukankan      html  css  js  c++  java
  • Android:weight,margin,padding详解实例

    标题图

    重量详解

    weight是用来等比例划分区域的属性。

    案例代码

    dashucoding

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    
    <LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:background="#ADFF2F"
    android:layout_weight="1"/>
    
    <LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:background="#DA70D6"
    android:layout_weight="2"/>
    
    </LinearLayout>

    dashucoding

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    
    <LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:background="#ADFF2F"
    android:layout_weight="1"/>
    
    <LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:background="#DA70D6"
    android:layout_weight="1"/>
    
    </LinearLayout>

    dashucoding

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/LinearLayout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal" >
    
      <TextView
      android:layout_weight="1"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:text="one"
      android:background="#98FB98"
      />
      <TextView
      android:layout_weight="2"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:text="two"
      android:background="#FFFF00"
     />
     <TextView
     android:layout_weight="3"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"  
     android:text="three"
     android:background="#FF00FF"
     />
    
    </LinearLayout>

    dashucoding

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/LinearLayout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="horizontal" >
    
      <TextView
      android:layout_weight="1"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:text="one"
      android:background="#98FB98"
      />
      <TextView
      android:layout_weight="2"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:text="two"
      android:background="#FFFF00"
     />
     <TextView
     android:layout_weight="3"
     android:layout_width="match_parent"
     android:layout_height="match_parent"  
     android:text="three"
     android:background="#FF00FF"
     />
    
    </LinearLayout>

    为什么出现2:1的案例呢3怎么就不见了呢?

    1. 每个宽度为match_parent,屏幕为1,那么屏幕就是1-3 = -2个math_parent
    2. 计算方法,1 - 2 * (1/6) = 2/3,1 - 2 * (2/6) = 1/3, 1 - 2 * (3/6) = 0结果是一占了两份,两占了一份,三什么都没有

    marginpadding详解

    margin代表是偏移,表示组件离容器偏移量,margin针对的是容器中的组件元素; padding代表是填充,填充的对象针对的是组件中的元素

    <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">
    
      <Button
      android:id="@+id/btn1"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:text="Button"/>
      <Button
      android:paddingLeft="100dp"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:text="Button"
      android:layout_toRightOf="@id/btn1"/>
    
      <Button
      android:id="@+id/btn2"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:text="Button"
      android:layout_alignParentBottom="true"/>
      <Button
      android:layout_marginLeft="100dp"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:text="Button"
      android:layout_toRightOf="@id/btn2"/>
    
    </RelativeLayout>

    结语

    • 本文主要讲解  机器人:重量,余量,填充详解实例
    • 下面我将继续对其他知识深入讲解,有兴趣可以继续关注
    • 小礼物走一走或点赞

    送❤

  • 相关阅读:
    WebView用法与JS交互(2) 响应webview中的图片点击事件
    出栈序列(栈和队列)
    Log Files
    Mr. Frog’s Game
    Comparison of Android versions
    Linearization of the kernel functions in SVM
    Game of Nuts
    Easy Summation
    Automatic Judge
    Coprime Sequence
  • 原文地址:https://www.cnblogs.com/dashucoding/p/11932502.html
Copyright © 2011-2022 走看看