首页 > 解决方案 > 如何以编程方式布局视图

问题描述

我正在尝试以编程方式添加线性布局,宽度是由约束垂直准则计算的。但是没有任何东西添加到布局中。我知道我需要先添加我的视图并克隆整个布局,然后开始链接每个元素,但我什至不认为我的指南正在创建。自定义控件用作预订创建者,用户可以在其中选择日期,用户界面加载用户可以预订的时间间隔。

我该如何调试呢?我哪里做错了?

片段.java

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.booker, container, false);  return rootView;  
}

片段.xml:

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/color_logo_sign_up" 
    android:orientation="vertical"> 

    <TextView 
        android:id="@+id/textView_Date" 
        android:layout_width="0dp" 
        android:layout_height="0dp" 
        android:gravity="center_horizontal" 
        android:text="Date" 
        android:textAppearance="@style/TextAppearance.AppCompat.Large" 
        android:textSize="36sp" 
        android:textStyle="bold" 
        app:layout_constraintBottom_toTopOf="@+id/textView_Month" 
        app:layout_constraintEnd_toEndOf="parent" 
        app:layout_constraintStart_toStartOf="parent" 
        app:layout_constraintTop_toTopOf="parent" />  

    <TextView 
        android:id="@+id/textView_Month" 
        android:layout_width="0dp" 
        android:layout_height="0dp" 
        android:gravity="center_horizontal" 
        android:text="Month" 
        android:textAppearance="@style/TextAppearance.AppCompat.Large" 
        android:textSize="24sp" android:textStyle="bold"
        app:layout_constraintBottom_toTopOf="@+id/recyclerView4" 
        app:layout_constraintEnd_toEndOf="parent" 
        app:layout_constraintStart_toStartOf="parent"   
        app:layout_constraintTop_toBottomOf="@+id/textView_Date" />  

   <android.support.v7.widget.RecyclerView 
        android:id="@+id/recyclerView4" 
        android:layout_width="match_parent" 
        android:layout_height="0dp"     
        app:layout_constraintBottom_toTopOf="@+id/guideline17" 
        app:layout_constraintEnd_toEndOf="parent" 
        app:layout_constraintStart_toStartOf="parent"          
        app:layout_constraintTop_toBottomOf="@+id/textView_Month" />  

    <com.techcloud.abdul.stutor.BookerView      
        android:layout_width="0dp" android:layout_height="0dp" 
        app:layout_constraintBottom_toBottomOf="parent" 
        app:layout_constraintEnd_toEndOf="parent" 
        app:layout_constraintStart_toStartOf="parent" 
        app:layout_constraintTop_toTopOf="@+id/guideline17"/> 

    <android.support.constraint.Guideline 
        android:id="@+id/guideline17" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:orientation="horizontal" 
        app:layout_constraintGuide_percent=".4" />   
</android.support.constraint.ConstraintLayout> 

布克.java

public class BookerView extends ConstraintLayout { 
Context context; 
AttributeSet attributeSet; 

public BookerView(Context context, @Nullable AttributeSet attrs) { 
    super(context, attrs);     
    this.context = context;     
    this.attributeSet = attrs;       
    init(context, attrs); 
} 

@Override 
protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    super.onLayout(changed,l,t,r,b); 
} 

private void init(Context context, AttributeSet attrs) { 
    int hours = 18; // starting at 6 am and ending at 12am 
    int width = getWidth();     
    int rowHeight = 100;      
    int id = 100; 
    LinearLayout linearLayoutTime = new LinearLayout(context);        

    linearLayoutTime.setBackgroundColor(getResources().getColor(R.color.colorPrimary));     
    linearLayoutTime.setId(id); 

    linearLayoutTime.setOrientation(LinearLayout.VERTICAL); int guideLineID = 101; 
    linearLayoutTime.setLayoutParams(new Constraints.LayoutParams(LayoutParams.MATCH_CONSTRAINT, LayoutParams.MATCH_CONSTRAINT));  

    ConstraintSet set = new ConstraintSet(); 
    int amountOfIntervalsPerHour = 1;

    if (attrs != null) { 
        TypedArray a = context.getTheme().obtainStyledAttributes( attrs, R.styleable.BookerView, 0, 0);    

        amountOfIntervalsPerHour = a.getInt(R.styleable.BookerView_amtOfIntervals,1); 
    } 

    addView(linearLayoutTime); 
    set.clone(this); 
    set.create(guideLineID, ConstraintSet.VERTICAL_GUIDELINE); 
    set.setGuidelinePercent(guideLineID, 0.5f); 
    set.connect(100, ConstraintSet.LEFT, this.getId(), ConstraintSet.LEFT, 0); 
    set.connect(100, ConstraintSet.RIGHT, guideLineID, ConstraintSet.RIGHT, 0); 
    set.connect(100, ConstraintSet.BOTTOM, this.getId(), ConstraintSet.BOTTOM, 0); 
    set.connect(100, ConstraintSet.TOP, this.getId(), ConstraintSet.TOP, 0); 
    set.applyTo(this); 
}  
}

标签: javaandroidandroid-custom-viewandroid-constraintlayoutcustom-view

解决方案


尝试这个:

我们已经熟悉的 ViewGroup 类——LinearLayout、TableLayout、RelativeLayout 等。这些 ViewGroup 类中的每一个都有 LayoutParams 内部类。这些 LayoutParams 的基类是 ViewGroup.LayoutParams。

像这样在java代码中以编程方式添加视图

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // creating LinearLayout
    LinearLayout linLayout = new LinearLayout(this);
    // specifying vertical orientation
    linLayout.setOrientation(LinearLayout.VERTICAL);
    // creating LayoutParams  
    LayoutParams linLayoutParam = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
    // set LinearLayout as a root element of the screen 
    setContentView(linLayout, linLayoutParam);

    LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    TextView tv = new TextView(this);
    tv.setText("TextView");
    tv.setLayoutParams(lpView);
    linLayout.addView(tv);

    Button btn = new Button(this);
    btn.setText("Button");
    linLayout.addView(btn, lpView);


    LinearLayout.LayoutParams leftMarginParams = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    leftMarginParams.leftMargin = 50;

    Button btn1 = new Button(this);
    btn1.setText("Button1");
    linLayout.addView(btn1, leftMarginParams);


    LinearLayout.LayoutParams rightGravityParams = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    rightGravityParams.gravity = Gravity.RIGHT;

    Button btn2 = new Button(this);
    btn2.setText("Button2");
    linLayout.addView(btn2, rightGravityParams);
}
}

ViewGroup.LayoutParams 只有两个属性:高度和宽度。它是子类 - ViewGroup.MarginLayoutParams 继承了这两个属性,并且有四个自己的属性:bottomMargin、leftMargin、rightMargin、topMargin。LinearLayout.LayoutParams 类,又是 ViewGroup 的子类。MarginLayoutParams,已经从它继承了 6 个属性,并添加了两个自己的属性:重力和重量。


推荐阅读