2017-01-20 1 views
0

나는 데이터베이스 값을 사용하여 프로그래밍 방식으로 안드로이드 뷰를 만들었습니다. 하지만 난 elemnts.LinearLayout을 만드는 방법을 잘 모르겠지만 제대로 정렬하지 않습니다. 내 실제 출력은프로그래밍 방식으로 안드로이드 요소 정렬

enter image description here

예상 출력입니다

enter image description here

내 코드

lView = new LinearLayout(Main2Activity.this); 
        // lView.setPadding(0,150,0,0); 
        lView.setBackgroundColor(Color.parseColor("#FFFFFF")); 
        lView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

LinearLayout.LayoutParams l2 = new   LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,  ViewGroup.LayoutParams.WRAP_CONTENT); 
        l2.gravity=Gravity.CENTER; 

        GradientDrawable gd3 = new GradientDrawable(); 
        gd3.setCornerRadius(30); 
        gd3.setColor(Color.parseColor("#003366")); 
        gd3.setStroke(0, 0xFF000000); 
        et1.setGravity(Gravity.CENTER); 
        et1.setHint("Select Date"); 
        et1.setBackgroundDrawable(gd3); 
         lView.setOrientation(LinearLayout.VERTICAL); 
        lView.addView(et1); 



        GradientDrawable gd4 = new GradientDrawable(); 
        gd4.setCornerRadius(30); 
        gd4.setColor(Color.parseColor("#5CB85C")); 
        gd4.setStroke(3, 0xFFFFFFFF); 
        Intime = new TextView(Main2Activity.this); 
        Intime.setHint("Select In Time"); 
        Intime.setTextColor(Color.WHITE); 
        Intime.setHintTextColor(Color.WHITE); 
        Intime.setTextSize(20); 
        Intime.setHeight(150); 
        Intime.setWidth(600); 
        Intime.setGravity(Gravity.CENTER); 
        Intime.setLayoutParams(l2); 
        Intime.setBackgroundDrawable(gd4); 
        lView.setOrientation(LinearLayout.HORIZONTAL); 
        lView.addView(Intime); 


        Outtime = new TextView(Main2Activity.this); 
        Outtime.setHint("Select Out Time"); 
        Outtime.setTextSize(20); 
        Outtime.setHeight(150); 
        Outtime.setWidth(600); 
        Outtime.setGravity(Gravity.CENTER); 
        Outtime.setTextColor(Color.WHITE); 
        Outtime.setHintTextColor(Color.WHITE); 
        Outtime.setLayoutParams(l2); 
        Outtime.setBackgroundDrawable(gd4); 
        lView.setOrientation(LinearLayout.HORIZONTAL); 
        lView.addView(Outtime); 
+0

중첩 된 LinearLayout 또는 RelativeLayout을 사용해야합니다. 네 스틱 된 LinearLayouts를 사용하는 경우, 상단 레이아웃은 수직 방향이어야하고 내부 레이아웃은 수평 방향이어야합니다. – Rachit

답변

1

편집처럼이.

lView = new LinearLayout(Main2Activity.this); 
         // lView.setPadding(0,150,0,0); 
         lView.setBackgroundColor(Color.parseColor("#FFFFFF")); 
         lView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

    LinearLayout.LayoutParams l2 = new   LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,  ViewGroup.LayoutParams.WRAP_CONTENT); 
         l2.gravity=Gravity.CENTER; 

         GradientDrawable gd3 = new GradientDrawable(); 
         gd3.setCornerRadius(30); 
         gd3.setColor(Color.parseColor("#003366")); 
         gd3.setStroke(0, 0xFF000000); 
         et1.setGravity(Gravity.CENTER); 
         et1.setHint("Select Date"); 
         et1.setBackgroundDrawable(gd3); 
          lView.setOrientation(LinearLayout.VERTICAL); 
         lView.addView(et1); 



         GradientDrawable gd4 = new GradientDrawable(); 
         gd4.setCornerRadius(30); 
         gd4.setColor(Color.parseColor("#5CB85C")); 
         gd4.setStroke(3, 0xFFFFFFFF); 
         Intime = new TextView(Main2Activity.this); 
         Intime.setHint("Select In Time"); 
         Intime.setTextColor(Color.WHITE); 
         Intime.setHintTextColor(Color.WHITE); 
         Intime.setTextSize(20); 
         Intime.setHeight(150); 
         Intime.setWidth(600); 
         Intime.setGravity(Gravity.CENTER); 
         Intime.setLayoutParams(l2); 
         Intime.setBackgroundDrawable(gd4); 

         // lView.addView(Intime); 


         Outtime = new TextView(Main2Activity.this); 
         Outtime.setHint("Select Out Time"); 
         Outtime.setTextSize(20); 
         Outtime.setHeight(150); 
         Outtime.setWidth(600); 
         Outtime.setGravity(Gravity.CENTER); 
         Outtime.setTextColor(Color.WHITE); 
         Outtime.setHintTextColor(Color.WHITE); 
         Outtime.setLayoutParams(l2); 
         Outtime.setBackgroundDrawable(gd4); 
         lView.setOrientation(LinearLayout.HORIZONTAL); 
         // lView.addView(Outtime); 


      LinearLayout lHorizontalView=new LinearLayout(Main2Activity.this); 


      LinearLayout.LayoutParams l3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,  ViewGroup.LayoutParams.WRAP_CONTENT); 
         l3.gravity=Gravity.CENTER; 


    lHorizontalView.setOrientation(LinearLayout.HORIZONTAL); 
    lHorizontalView.setLayoutParams(l3); 
    lHorizontalView.addView(Intime); 
    lHorizontalView.addView(Outtime); 

     lView.addView(lHorizontalView); 
+0

감사합니다. 작동 중입니다 –

+0

환영합니다 :) 해피 코딩 .. –

+0

안녕하세요. 왼쪽에 정렬되어 있으며 화면 크기에 따라 다릅니다. 어떤 해결책이 있습니까? –

0

당신은 XML 샘플이 코드 출력을 정렬을 수행해야합니다

https://i.stack.imgur.com/Kln1g.png

<LinearLayout 
android:id="@+id/linearLayout4" 
android:layout_width="wrap_content" 
android:layout_height="fill_parent" 
android:layout_below="@+id/linearLayout3" 
android:layout_centerHorizontal="true" 
android:orientation="horizontal" 
android:gravity="bottom" 
android:layout_alignParentBottom="true" 
android:layout_marginTop="20dp"> 
<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" /> 
<Button 
    android:id="@+id/button2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button" /></LinearLayout> 
+0

나는 프로그래밍 방식으로 요소를 만들고 있습니다. xml 파일을 사용하고 있지 않습니다. 페이지가 동적이기 때문에 xml을 사용할 수 없습니다. –

+0

중력 센터를 왼쪽 또는 오른쪽으로 시도 했습니까? –

관련 문제