【问题标题】:Custom Linear Layout Child not showing自定义线性布局子不显示
【发布时间】:2016-08-06 03:49:48
【问题描述】:

我尝试创建具有动态高度和宽度 = 高度的自定义线性布局。自定义布局很好,但子视图没有显示在我的自定义线性布局中。我尝试了一些方法来解决它,但仍然无法解决它。

public class CustomLinearLayout extends LinearLayout {
    public CustomLinearLayout(Context context) {
        super(context);
    }

    public CustomLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(height, height);

    }
}

这是包含我的自定义线性布局的 xml。子视图未显示。

<com.example.admin.antriclient.CustomLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:autofit="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="@drawable/bg_nomor_antrian"
    android:padding="10dp"
    >
        <me.grantland.widget.AutofitTextView
            android:id="@+id/service_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Teller"
            android:textColor="@color/white"
            android:textStyle="bold"
            android:gravity="center_horizontal"
            android:singleLine="false"
            autofit:minTextSize="12sp"
            />
        <me.grantland.widget.AutofitTextView
            android:id="@+id/nomor_antrian"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="T21"
            android:textSize="50sp"
            android:textColor="@color/white"
            android:textStyle="bold"
            android:gravity="center_horizontal"
            android:singleLine="true"
            autofit:minTextSize="12sp"
            />

</com.example.admin.antriclient.CustomLinearLayout>

提前致谢

【问题讨论】:

  • 如果您不调用onMeasure() 中的super 方法,则需要通过适当地调用measure() 方法来处理每个子View 的测量。否则,子 Views 的宽度和高度将为 0。

标签: android android-linearlayout android-custom-view


【解决方案1】:

对我来说,我必须添加一个 LayoutInflater 才能显示

public class SidebarLayout extends LinearLayout {

    private static final String TAG = "SidebarLayout";

    public SidebarLayout(Context context) {
        super(context);    
    }

    public SidebarLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);       
        LayoutInflater.from(context).inflate(R.layout.sidebar, this);
    }

    public SidebarLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);      
    }
}
<com.example.a3danimationtester.SidebarLayout
        android:id="@+id/layout_sidebar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/layout_topbar" />

【讨论】:

    【解决方案2】:

    我的问题是因为我没有调用 super.onMeasure() 方法。感谢 Mike M。

    我的课应该是这样的

    public class CustomLinearLayout extends LinearLayout {
        public CustomLinearLayout(Context context) {
            super(context);
        }
    
        public CustomLinearLayout(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(heightMeasureSpec,heightMeasureSpec);
            //int height = MeasureSpec.getSize(heightMeasureSpec);
            //setMeasuredDimension(height, height);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多