使用android API 21更改按钮和编辑文本的大小

使用android API 21更改按钮和编辑文本的大小,android,android-5.0-lollipop,Android,Android 5.0 Lollipop,我正在开发一个android应用程序,它与API 13到19配合得很好,当我用API 21测试它时,按钮大小、edittext和textview都发生了变化(变大了)。我正在使用(match_parent和wrap_content)。我在网上发现,我应该添加以下行android:textAllCaps=“false” android:stateListAnimator=“@null”但我得到了相同的结果 这是我的xml文件: <?xml version="1.0" encoding="ut

我正在开发一个android应用程序,它与API 13到19配合得很好,当我用API 21测试它时,按钮大小、edittext和textview都发生了变化(变大了)。我正在使用(match_parent和wrap_content)。我在网上发现,我应该添加以下行
android:textAllCaps=“false”
android:stateListAnimator=“@null”
但我得到了相同的结果 这是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#ffffff"
xmlns:android="http://schemas.android.com/apk/res/android" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:textAllCaps="false"
android:stateListAnimator="@null"
android:layout_height="fill_parent"
android:background="#ffff" >

<ImageView
    android:id="@+id/logimage"
    android:layout_width="387px"
    android:layout_height="180px"
    android:textAllCaps="false"
    android:stateListAnimator="@null"
    android:layout_marginTop="40px"
    android:src="@drawable/logo_nearby"
    android:layout_centerHorizontal="true"
    />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btnstart"
    android:textSize="13sp"
    android:textAllCaps="false"
    android:stateListAnimator="@null"
    android:background="#8cd2fc"
    android:textColor="#ffffff"
    android:singleLine="true"
    android:layout_marginTop="80px"
    android:layout_centerHorizontal="true"
    android:text="Create account"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:layout_below="@id/logimage"/>

<Button
    android:layout_width="fill_parent"
    android:id="@+id/btnanonym"
    android:textSize="13sp"
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/btnstart"
    android:layout_marginTop="30px"
    android:text="Connect as anonyme"
    android:background="#00b4e8"
    android:singleLine="true"
    android:textAllCaps="false"
    android:stateListAnimator="@null"
    android:textColor="#ffffff"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:layout_height="wrap_content" />
<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/btncnxuser"
    android:background="#00b4e8"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:layout_centerHorizontal="true"
    android:text="Connect as member"
    android:textColor="#ffffff"
    android:textAllCaps="false"
    android:stateListAnimator="@null"
    android:textSize="13sp"
    android:singleLine="true"
    android:layout_marginTop="85px"
    android:layout_below="@id/btnanonym"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10px"
    android:id="@+id/txtinfo"
    android:text="You have already account ?"
    android:textColor="#000"
    android:singleLine="true"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/btnanonym"/>
<com.facebook.login.widget.LoginButton
    android:layout_width="match_parent"
    android:id="@+id/logButton"
    android:layout_marginTop="30px"
    android:textAllCaps="false"
    android:stateListAnimator="@null"
    android:background="#3c5f9d"
    android:singleLine="true"
    android:text="Login with Facebook"
    android:textSize="12sp"
    android:textColor="#ffffff"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:layout_below="@+id/btncnxuser"
    android:layout_height="wrap_content">
</com.facebook.login.widget.LoginButton>

使用Edittext的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#ffffff"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff" >

<ImageView
    android:id="@+id/logimage"
    android:layout_width="187sp"
    android:layout_height="50dp"
    android:layout_marginTop="20dp"
    android:src="@drawable/logo_nearby"
    android:layout_centerHorizontal="true"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="43dp"
    android:id="@+id/msgerror"
    android:textSize="6dp"
    android:layout_below="@id/logimage"
    android:textColor="#FF0000"
    android:layout_centerHorizontal="true"
    />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:inputType="textPersonName"
    android:hint="First name"
    android:padding="5dp"
    android:textSize="17dp"
    android:textColorHint="@color/hinttxt"
    android:background="@color/bluehint"
    android:singleLine="true"
    android:id="@+id/txtfirstn"
    android:layout_below="@id/msgerror"
    android:layout_centerHorizontal="true"
    />

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:hint="Last name"
    android:textSize="17dp"
    android:layout_marginTop="10dp"
    android:padding="5dp"
    android:singleLine="true"

    android:textColorHint="@color/hinttxt"
    android:background="@color/bluehint"
    android:id="@+id/txtlastn"
    android:layout_below="@id/txtfirstn"
    android:layout_centerHorizontal="true"
    />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:hint="Email"
    android:layout_marginTop="10dp"
    android:textColorHint="@color/hinttxt"
    android:background="@color/bluehint"
    android:inputType="textEmailAddress"
    android:padding="5dp"
    android:textSize="17dp"
    android:singleLine="true"
    android:id="@+id/txtemail"
    android:layout_below="@id/txtlastn"

    />


<Spinner
    android:id="@+id/my_spinner"
    android:layout_width="match_parent"
    android:spinnerMode="dialog"
    android:prompt="@string/prompt"
    android:layout_below="@id/txtemail"
    android:popupElevation="@drawable/rounded_corner"
    android:layout_height="wrap_content" />


<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="phone"
    android:hint="Phone"
    android:padding="5dp"
    android:textSize="17dp"
    android:singleLine="true"

    android:textColorHint="@color/hinttxt"
    android:background="@color/bluehint"
    android:id="@+id/txtphone"
    android:layout_below="@id/my_spinner"
    android:layout_centerHorizontal="true" />
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:hint="Your password"
    android:layout_marginTop="10dp"
    android:inputType="textPassword"
    android:padding="5dp"
    android:singleLine="true"
    android:textSize="17dp"
    android:textColorHint="@color/hinttxt"
    android:background="@color/bluehint"
    android:id="@+id/txtpwd"
    android:layout_below="@id/txtphone"
    />


<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="17dp"
    android:layout_centerHorizontal="true"
    android:hint="Re type your password"
    android:inputType="textPassword"
    android:padding="5dp"
    android:singleLine="true"
    android:layout_marginTop="10dp"
    android:id="@+id/txtrepwd"
    android:textColorHint="@color/hinttxt"
    android:background="@color/bluehint"
    android:layout_below="@id/txtpwd"
    />



<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Register"
    android:textSize="13sp"
    android:textColor="#ffffff"
    android:background="@color/bluedrawer"
    android:id="@+id/register"
    android:layout_centerHorizontal="true"
    android:layout_below="@id/txtrepwd"
    android:layout_centerInParent="true"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:layout_marginTop="20dp"
    />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/register"
    android:visibility="gone"
    android:id="@+id/idH"
    />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    android:layout_below="@id/idH"
    android:id="@+id/title"
    />

</RelativeLayout>
</ScrollView>

您必须使用dp/dip,而不是使用px和sp。根据手机默认样式和分辨率或版本>19,它不会变大


这种做法将有助于您获得一致的观点

您在
按钮和
文本视图中使用的是
px
作为单位,您应该使用
dp
。问题可能就在那里

在您的
EditText
中,您正在设置
android:layout\u width=“match\u parent”
,因此它将与父视图的宽度相同。例如,如果需要设置特定的宽度,请将
android:layout\u height=“wrap\u content”
更改为所需的大小

android:layout_height="128dp"

如果在
按钮和
文本视图中使用as unit
px
,则应改用
dp
。可能是有问题。@algui91谢谢你,它工作了,如果我没有定义任何文本大小,设备会决定每个屏幕大小的正确大小吗?好的,我将添加它作为答案。谢谢你的回答。它会给versoin带来问题吗?不,它不会给版本<19带来任何问题。很抱歉,我还有一个问题,关于editText,我使用了dp,但大小总是大的。如果editText变大,那么它将为所有api版本显示大的,而不是单个api 21。根据API 21,代码可能不会变化。我正在使用API 15和API 21进行测试,并且edittext的大小会发生变化。我不明白为什么?