滚动视图不会以编程方式滚动设置高度
scrollview doesn't scroll setting height programmatically
我会从我的目标开始。我想要实现的是布局比 phone.
的实际屏幕大 3 倍
现在我尝试测试我的解决方案,但我遇到了滚动问题。
这就是我的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="400dp"
tools:context=".Zo"
android:id="@+id/zo_root"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="9">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="@color/Red"
></FrameLayout >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="@color/Yellow"
></FrameLayout >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="@color/Blue"
></FrameLayout >
</LinearLayout>
</ScrollView>
我背后的代码:
@Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zo);
setParameterForScroolView();
}
private void setParameterForScroolView()
{
ParameterKeeper parameterKeeper = new ParameterKeeper(this);
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.zo_root);
linearLayout.getLayoutParams().height = parameterKeeper.getHeightOfScreen() *3;
}
后面的代码运行良好,导致我的整个屏幕都是红色的,但我根本无法滚动。
有answer which said, that I need to remove from scrollView
android:fillViewport="true"
. After that my LinearLayout diseapers like in this个问题。
我将 LinearLayout 添加为 root (source),但它对我一点帮助都没有。
你有什么解决办法吗?
我也尝试为 xml
中的根 LinearLayout
设置 height
1600dp
而不是 400dp
以确保重新计算 [=] 没有问题19=]。也不行。
您正在更改错误布局的大小。你想修改滚动视图中第一个布局的高度。
如果子元素具有 layout_weight 并且我以编程方式更改父视图中的高度值,则滚动无法正常工作。
在我的例子中,让它工作的唯一方法是在 activity 的代码中将每个 frame layout
的高度设置为等于屏幕大小。这样我就可以达到我的目标 layout height == 3 x real screen size
我会从我的目标开始。我想要实现的是布局比 phone.
的实际屏幕大 3 倍现在我尝试测试我的解决方案,但我遇到了滚动问题。 这就是我的布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="400dp"
tools:context=".Zo"
android:id="@+id/zo_root"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="9">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="@color/Red"
></FrameLayout >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="@color/Yellow"
></FrameLayout >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="@color/Blue"
></FrameLayout >
</LinearLayout>
</ScrollView>
我背后的代码:
@Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_zo);
setParameterForScroolView();
}
private void setParameterForScroolView()
{
ParameterKeeper parameterKeeper = new ParameterKeeper(this);
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.zo_root);
linearLayout.getLayoutParams().height = parameterKeeper.getHeightOfScreen() *3;
}
后面的代码运行良好,导致我的整个屏幕都是红色的,但我根本无法滚动。
有answer which said, that I need to remove from scrollView
android:fillViewport="true"
. After that my LinearLayout diseapers like in this个问题。
我将 LinearLayout 添加为 root (source),但它对我一点帮助都没有。
你有什么解决办法吗?
我也尝试为 xml
中的根 LinearLayout
设置 height
1600dp
而不是 400dp
以确保重新计算 [=] 没有问题19=]。也不行。
您正在更改错误布局的大小。你想修改滚动视图中第一个布局的高度。
如果子元素具有 layout_weight 并且我以编程方式更改父视图中的高度值,则滚动无法正常工作。
在我的例子中,让它工作的唯一方法是在 activity 的代码中将每个 frame layout
的高度设置为等于屏幕大小。这样我就可以达到我的目标 layout height == 3 x real screen size