Android RelativeLayout padding 的一个坑?

记录最近无意间使用RelativeLayout的padding top属性时,发现的一个坑。

正常的ViewGroup 控件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="center"
        android:background="@android:color/black"
        android:paddingTop="40dp">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="10dp"
            android:layout_gravity="center"
            android:layout_marginStart="33dp"
            android:background="@android:color/white"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@android:color/holo_green_dark" />

    </FrameLayout>
</FrameLayout>

这些普通正常的布局得到的效果如下:

这里写图片描述

可以看到加了padding top 之后内容的居中为在除去padding的真实内容显示区域内居中,这是普通正常的ViewGroup控件。

特立独行的RelativeLayout

将上述的内层FrameLayout替换为RelativeLayout,改改数值方便看到效果:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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.jokerlee.androidlsample.Main2Activity">

    <RelativeLayout
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:background="@android:color/black"
        android:paddingTop="30dp">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="20dp"
            android:layout_centerVertical="true"
            android:layout_marginStart="33dp"
            android:background="@android:color/white"/>

        <View
            android:layout_width="match_parent"
            android:layout_height="2dp"
            android:background="@android:color/holo_green_dark"/>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true" />

    </RelativeLayout>

</FrameLayout>

效果如下:
这里写图片描述

其居中稳稳的把持在了整个ViewGroup高度的中央,其padding top的部分在居中时完全没有用,坑爹了word RelativeLayout。

PS:博主是在使用顶部RelativeLayout android:fitsSystemWindows=”true”属性时,恰好就在该RelativeLayout内使用了居中属性,一直在怀疑是fitsSystemWindows有使用限制,没成想是被RelativeLayout坑了。。。。。。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,下面是步骤: 1. 打开Android Studio,创建一个新的项目。 2. 在app目录下的res文件夹中,创建一个新的layout文件夹。在这个文件夹下创建一个新的xml文件,命名为activity_login.xml。 3. 在xml文件中添加以下代码: ``` <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:src="@drawable/qq_logo" /> <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/logo" android:layout_marginTop="50dp" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:padding="10dp" android:hint="请输入QQ号" /> <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/username" android:layout_marginTop="20dp" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:padding="10dp" android:inputType="textPassword" android:hint="请输入密码" /> <Button android:id="@+id/login_button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/password" android:layout_marginTop="50dp" android:layout_marginLeft="30dp" android:layout_marginRight="30dp" android:text="登录" /> </RelativeLayout> ``` 4. 在res文件夹下的drawable文件夹中,添加一个新的图片,命名为qq_logo.png,作为QQ的logo图标。 5. 在MainActivity.java文件中,添加以下代码: ``` public class MainActivity extends AppCompatActivity { private EditText usernameEditText; private EditText passwordEditText; private Button loginButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); usernameEditText = findViewById(R.id.username); passwordEditText = findViewById(R.id.password); loginButton = findViewById(R.id.login_button); loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String username = usernameEditText.getText().toString(); String password = passwordEditText.getText().toString(); // 在这里添加登录的逻辑 } }); } } ``` 6. 运行程序,你就可以看到一个仿制的QQ登录界面了。 注意:这个登录界面只是一个简单的示例,没有实现真正的登录功能。如果你想要实现登录功能,你需要自己编写相关的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值