如何以编程方式布置视图

时间:2018-08-16 02:24:38

标签: java android android-custom-view android-constraintlayout custom-view

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

我该如何调试?我哪里出问题了?

fragment.java

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

Fragment.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> 

Booker.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); 
}  
}

1 个答案:

答案 0 :(得分:0)

尝试一下:

我们已经熟悉的

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只有两个属性: height和width。。它是子类-ViewGroup.MarginLayoutParams继承了这两个属性,并且具有四个自己的属性: bottomMargin,leftMargin,rightMargin,topMargin。 LinearLayout.LayoutParams类,该类又是ViewGroup的子类。 MarginLayoutParams,已经从中继承了6个属性,并添加了两个属性:重力和重量。