Android 以编程方式固定页面底部的相对布局
我已经创建了一个scrollView,其中包含我活动的所有布局。在scrollview内部,我创建了一个垂直方向的线性布局,在它内部,我创建了一些包含图像和文本视图的相对布局。我需要scrollview,因为在某些时刻,我可以在布局中有许多图像,屏幕需要滚动。 这没问题,一切正常。我的问题在后面。正如您在代码底部看到的,我创建了最后一个包含简单按钮的相对布局。我的问题是,这个布局并不停留在页面的底部,但它会相对于屏幕上下移动。因此,如果只有一个图像,例如,最后一个相对布局位于页面顶部,附加到图像。如果有许多图像,则位于页面底部。 我想做的是,最后一个相对布局始终保持在页面底部,如果屏幕中只有一个图像也是如此。 如何修改代码以实现目标Android 以编程方式固定页面底部的相对布局,android,android-layout,Android,Android Layout,我已经创建了一个scrollView,其中包含我活动的所有布局。在scrollview内部,我创建了一个垂直方向的线性布局,在它内部,我创建了一些包含图像和文本视图的相对布局。我需要scrollview,因为在某些时刻,我可以在布局中有许多图像,屏幕需要滚动。 这没问题,一切正常。我的问题在后面。正如您在代码底部看到的,我创建了最后一个包含简单按钮的相对布局。我的问题是,这个布局并不停留在页面的底部,但它会相对于屏幕上下移动。因此,如果只有一个图像,例如,最后一个相对布局位于页面顶部,附加到图像
//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);
}
//滚动视图
ScrollView ScrollView=新的ScrollView(此)//创建一个新的滚动视图
setBackground(getResources().getDrawable(R.drawable.background))//给背景渐变色
scrollView.setLayoutParams(新的scrollView.LayoutParams(scrollView.LayoutParams.MATCH_父项)//设置有关scrollView动态大小的主参数
ScrollView.LayoutParams.MATCH_PARENT);
设置填充(0,20,0,0);
//线性布局
LinearLayout LinearLayout=新的LinearLayout(本)//创建新的线性布局
linearLayout.setOrientation(linearLayout.VERTICAL)//设置布局方向
linearLayout.setLayoutParams(新的linearLayout.LayoutParams(linearLayout.LayoutParams.MATCH_父级,
LinearLayout.LayoutParams.WRAP_内容);
对于(i=0;i您可以有一个相对布局,在它里面您应该将您的scrollview和button.botton放在底部,scrollview放在相对布局的顶部。如果您这样做,您将始终将按钮放在屏幕底部
<RelativeLayout>
<ScrollView> <!--set scrollview in top of relativelayout and top of button -->
<LinearLayout>
.
.
.
</LinearLayout>
</ScrollView>
<Button/> <!--set button in buttom of relativelayout-->
</RelativeLayout>
.
.
.
很抱歉,我没有正确理解。请您解释清楚好吗?您应该在主布局中有一个RelativeLayout,然后将您的scrollview和按钮放在您想要的位置。您应该将按钮放在scrollview的外部和该RelativeLayout和buttom的内部