Java 使用ScrollView进行自动滚动,并且不通过RelativeLayout.LayoutParams获取页边距底部
我试图在Java 使用ScrollView进行自动滚动,并且不通过RelativeLayout.LayoutParams获取页边距底部,java,android,Java,Android,我试图在线性布局中显示10个绿色框,我面临两个问题。我已经通过params.setMargins(0,0,0,100)定义了边距底部,但它不起作用。另一个问题是,我正在使用.addView()通过MainActivity.java插入10个绿色框,当它们被插入后,ScrollView显示滚动位于底部。因此,当应用程序打开时,屏幕上会显示第10个绿色框,我希望第一个绿色框出现在那里,那么我如何才能关闭此自动滚动 activity\u main.xml: <?xml version="1.0"
线性布局中显示10个绿色框,我面临两个问题。我已经通过params.setMargins(0,0,0,100)
定义了边距底部,但它不起作用。另一个问题是,我正在使用.addView()
通过MainActivity.java
插入10个绿色框,当它们被插入后,ScrollView
显示滚动位于底部。因此,当应用程序打开时,屏幕上会显示第10个绿色框,我希望第一个绿色框出现在那里,那么我如何才能关闭此自动滚动
activity\u main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
由于您将视图定义为添加到“内容”(即LinearLayout)上,因此创建的LayoutParams也将是线性的,这将解决此问题。其余的都一样
以下是我对您的更改:(在java类中)
LinearLayout content=findviewbyd(R.id.content);
对于(int x=0;x)“但它不工作”的确切含义是什么?页边距底部没有显示made@JustinJasmann或者是为你做的?
package com.example.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout content = findViewById(R.id.content);
for (int x=0;x<10;x++) {
RelativeLayout element = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 400);
params.setMargins(0, 0, 0, 100); // NOT GETTING THE MARGIN BOTTOM
element.setLayoutParams(params);
element.setBackgroundResource(R.color.green);
content.addView(element);
}
}
}
LinearLayout content = findViewById(R.id.content);
for (int x=0;x<10;x++) {
RelativeLayout element = new RelativeLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 400);
params.setMargins(0, 0, 0, 100); // now should work
element.setLayoutParams(params);
element.setBackgroundResource(R.color.green);
content.addView(element);
}