2013-10-16 52 views
0

我創建了一個包含我所有活動佈局的scrollView。在scrollview中,我創建了一個垂直方向的線性佈局,並在其內部使用for-loop創建了一些包含圖像和textview的相對佈局。我需要滾動視圖,因爲在某些時候,我可以在佈局中有許多圖像,並且屏幕需要滾動。 這是好的,所有工作都正常。我的問題在之後。正如你可以在代碼的底部看到的,我創建了包含一個簡單按鈕的最後一個相對佈局。我的問題是,這種佈局不會停留在頁面的底部,而是會在與屏幕的關係上上下移動。因此,例如,如果只有一個圖像,則最後一個相對佈局位於頁面頂部,並附加到圖像上。如果有許多圖像,它位於頁面的底部。 我想要做的是,最後的相對佈局總是保持在頁面的底部,同樣如果屏幕內只有一個圖像。 如何修改我的代碼以實現我的目標?以編程方式修復頁面底部的相對佈局

  //SCROLL VIEW 
      ScrollView scrollView = new ScrollView(this); //create a new scrollView 
      scrollView.setBackground(getResources().getDrawable(R.drawable.background)); //give the background gradient 
      scrollView.setLayoutParams(new ScrollView.LayoutParams(ScrollView.LayoutParams.MATCH_PARENT, //set the main params about the dynamic size of the scrollView 
                 ScrollView.LayoutParams.MATCH_PARENT)); 
      scrollView.setPadding(0, 20, 0, 0); 

      //LINEAR LAYOUT 
      LinearLayout linearLayout = new LinearLayout(this); //create a new linearLayout 
      linearLayout.setOrientation(LinearLayout.VERTICAL); //set the layout orientation 
      linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT)); 

      for (i=0; i<= 3; i++) { 

       //RELATIVE LAYOUT 
       RelativeLayout relativeLayout = new RelativeLayout(this); //create a new relative layout 
       relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, //set main params about the width and height 
         RelativeLayout.LayoutParams.FILL_PARENT)); 
       relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor)); //set background color 
       LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(
         new LinearLayout.LayoutParams(
           LinearLayout.LayoutParams.MATCH_PARENT, 
           LinearLayout.LayoutParams.WRAP_CONTENT)); 
       relativeParams.setMargins(20, 20, 20, 0); 
       relativeLayout.setLayoutParams(relativeParams); //set declared params about layout to the relativeLayout 
       relativeLayout.requestLayout(); 

       //IMAGE VIEW 
       ImageView selectedPhoto = new ImageView(this); //create a new imageView 
       //imageView code here 

       //TEXT VIEWS 
       TextView numberCopies = new TextView(this); //create new TextView 
       numberCopies.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); 
       numberCopies.setGravity(Gravity.CENTER); //set position to the center in confront to the parent 
            RelativeLayout.LayoutParams layoutParamsNumberCopies = (RelativeLayout.LayoutParams) numberCopies.getLayoutParams(); 
       layoutParamsNumberCopies.addRule(RelativeLayout.CENTER_HORIZONTAL); //add a rule to the layout params. We put his position at the horizontal center of the relative layout 
       numberCopies.setLayoutParams(layoutParamsNumberCopies); //set the layout rules to the textView 

       TextView priceCopies = new TextView(this); 
       priceCopies.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); 
       priceCopies.setGravity(Gravity.CENTER); 
       numberCopies.setPadding(25, 25, 25, 25); 
       priceCopies.setTextColor(getResources().getColor(R.color.redColor)); 

       RelativeLayout.LayoutParams layoutParamsPriceCopies = (RelativeLayout.LayoutParams) priceCopies.getLayoutParams(); 
       layoutParamsPriceCopies.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
       layoutParamsPriceCopies.addRule(RelativeLayout.CENTER_VERTICAL); 
       priceCopies.setLayoutParams(layoutParamsPriceCopies); 

       relativeLayout.addView(selectedPhoto); 
       relativeLayout.addView(numberCopies); 
       relativeLayout.addView(priceCopies); 
       linearLayout.addView(relativeLayout); 
      } 

      //RELATIVE LAYOUT 
      RelativeLayout relativeLayoutOpenButton = new RelativeLayout(this); //create a new relative layout for add the main buttons 
      relativeLayoutOpenButton.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, //add the params for the width and height 
        RelativeLayout.LayoutParams.WRAP_CONTENT)); 
      relativeLayoutOpenButton.setBackgroundColor(getResources().getColor(R.color.blackColor)); //set the black background 
      relativeLayoutOpenButton.setPadding(10, 10, 10, 10); //set the padding 
      LinearLayout.LayoutParams relativeParamsOpenButton = new LinearLayout.LayoutParams(
        new LinearLayout.LayoutParams(
          LinearLayout.LayoutParams.MATCH_PARENT, 
          LinearLayout.LayoutParams.WRAP_CONTENT)); 
      relativeParamsOpenButton.setMargins(0, 20, 0, 0); //put a top margin for separate the black bar from the last image line 
      relativeParamsOpenButton.gravity = Gravity.BOTTOM; //set the gravity to the bottom 
      relativeLayoutOpenButton.setLayoutParams(relativeParamsOpenButton); 
      relativeLayoutOpenButton.requestLayout(); 

      Button confirmButton = new Button(this); //create a new button 
      //code button here 
      relativeLayoutOpenButton.addView(confirmButton); //add the button to the view 

      scrollView.addView(linearLayout); 
      setContentView(scrollView); 
     } 

感謝

回答

0

你可以有一個相對的佈局,它的內部layout.if你這樣做,你將有永遠是你應該把你在你相對頂部和底部滾動視圖滾動視圖和button.botton屏幕底部的按鈕

<RelativeLayout> 
<ScrollView> <!--set scrollview in top of relativelayout and top of button --> 
<LinearLayout> 
. 
. 
. 
</LinearLayout> 
</ScrollView> 

<Button/> <!--set button in buttom of relativelayout--> 
</RelativeLayout> 
+0

對不起,但我沒有正確理解。你能解釋得更好嗎? – Hieicker

+1

你應該在你的主佈局中有一個RelativeLayout,然後在其中放置你想要的滾動視圖和按鈕。你應該將按鈕放置在滾動視圖的外部以及它的相對佈局和按鈕內部 – zohreh

+0

我編輯我的答案。我希望它幫你 – zohreh

相關問題