其他分享
首页 > 其他分享> > 布局中的Android按钮高度未填充父级

布局中的Android按钮高度未填充父级

作者:互联网

我创建了这个布局,我不知道为什么Button的高度没有填充布局高度

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
    >

  <View
      android:layout_width="match_parent"
      android:layout_height="1dp"
      android:background="@color/pf_sm_dividor_light"
      />

  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:layout_toLeftOf="@+id/button">

      <ImageView
          android:id="@+id/li_icon"
          android:layout_width="24dp"
          android:layout_height="24dp"
          android:layout_centerVertical="true"
          android:src="@drawable/pf_ic_main"
          android:layout_alignParentLeft="true"
          android:scaleType="centerInside"
          android:layout_marginLeft="9dp"
          android:contentDescription="@string/pf_app_name" />

      <TextView
          style="@style/pf.TextView.sm_title"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_centerVertical="true"
          android:gravity="center_vertical|left"
          android:layout_marginLeft="2dp"
          android:text="Text Here"
          android:layout_toRightOf="@id/li_icon"
          android:layout_alignRight="@+id/pf_sm_ktext" />

      <ir.system.views.KText
          style="@style/pf.TextView.sm_title"
          android:layout_width="wrap_content"
          android:textStyle="normal"
          android:layout_height="wrap_content"
          android:gravity="center_vertical|right"
          android:text="1,500"
          android:textSize="22sp"
          android:layout_centerVertical="true"
          android:layout_alignParentRight="true"
          android:id="@+id/pf_sm_ktext" />

    </RelativeLayout>
<!-- Here is the Button -->
    <Button
        android:layout_alignParentRight="true"
        android:layout_width="60dp"
        android:layout_margin="0dp"
        android:text="+"
        android:layout_height="match_parent"
        android:background="@drawable/pf_btn"
        android:id="@+id/button"
        />
  </RelativeLayout>

  <View
      android:layout_width="match_parent"
      android:layout_height="1dp"
      android:background="@color/pf_sm_dividor_dark"
      />
</LinearLayout>

但结果是这样的:

模拟器android 4.0结果:

解决方法:

你的问题的基本概念..

<LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >



        <ImageButton 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:src="@drawable/ic_launcher"
            />


        </LinearLayout>

使用相对布局试试这个:

<RelativeLayout
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      >

      <ImageView
          android:id="@+id/li_icon"
          android:layout_width="24dp"
          android:layout_height="24dp"
          android:layout_centerVertical="true"
          android:layout_alignParentLeft="true"
          android:scaleType="centerInside"
          android:layout_marginLeft="9dp" 
          android:src="@drawable/ic_launcher"
          />

      <TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_centerVertical="true"
          android:gravity="center_vertical|left"
          android:layout_marginLeft="2dp"
          android:text="Text Here"
          android:layout_toRightOf="@id/li_icon"
          android:layout_alignRight="@+id/pf_sm_ktext" />




<!-- Here is the Button -->
    <Button
        android:layout_alignParentRight="true"
        android:layout_width="60dp"
        android:layout_margin="0dp"
        android:text="+"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_toLeftOf="@+id/rl"
        />
  </RelativeLayout>

使用线性布局试试这个

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="horizontal"
                  android:background="@color/red"
        >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:layout_gravity="start"
            />

        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Android Hacker"
            android:layout_weight="1"
            android:textSize="15sp"
            />

        <LinearLayout 
            android:layout_width="1dp"
            android:layout_height="fill_parent"
            android:background="@color/black"

            ></LinearLayout>

        <Button 
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="okay"
            android:layout_gravity="end"
            android:background="@color/brown"
            />


    </LinearLayout>

标签:android,android-layout,android-button
来源: https://codeday.me/bug/20191007/1865945.html