Where did my 10dp height button goes?

2019-07-26 23:56发布

问题:

I have coded as below for 3 devices (images below), and log the Density/Resolution/OS (so that you know which device is which). I have 5 buttons and 5 images, each with height from 10dp to 50dp.

You'll notice the images appears on all devices. All the buttons appear on on XXXHDPI device (which is a Samsung S7 device), but for on XXHDPI (Nexus 5) and XHDPI (Samsung S3), the 10dp button disappeared. Why?

The images as below

And my layout as below (no other special code, just the default Empty Activity project).

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.elyeproj.phoneinfo.MainActivity">

    <TextView
        android:id="@+id/resolution_width"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/resolution_height"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/resolution_width_dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/resolution_height_dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/resolution_density"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/resolution_density_str"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/phone_os"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="10dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="20dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="30dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="40dp" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:layout_marginBottom="5dp"
        android:background="@android:color/holo_blue_dark"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:layout_marginBottom="5dp"
        android:background="@android:color/holo_blue_dark"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginBottom="5dp"
        android:background="@android:color/holo_blue_dark"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginBottom="5dp"
        android:background="@android:color/holo_blue_dark"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/holo_blue_dark"/>
</LinearLayout>

回答1:

Better try to give separate LinearLayout for text view, button and image. Use android layout weight for that LinearLayout... hope it will work



回答2:

the "problem" is in default button background and its internal paddings. To fix - set some color to buttons background

for example

android:background="@android:color/holo_red_dark"