【发布时间】:2020-12-22 15:35:36
【问题描述】:
我的 Android 应用中有几个可扩展的布局。当我单击展开布局时,布局会在屏幕外消失,我必须手动向下滚动以使其可见。如何让 ScrollView 自动向下滚动以使点击的布局可见?
我尝试使用scrollView.scrollTo(0, textID.getBottom()); 滚动到布局元素的底部,但没有成功。
Java:
expandableLayout1 = root.findViewById(R.id.expandable_layout1);
button = (LinearLayout)root.findViewById(R.id.box_header);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
if (expandableLayout1.isExpanded()) {
expandableLayout1.collapse();
} else {
expandableLayout1.expand();
scrollView.scrollTo(0, textID.getBottom());
}
}
});
xml:
<LinearLayout
android:id="@+id/box"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/box_header"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/textID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="title"
android:textColor="#ffffff"
android:textSize="16sp"/>
</LinearLayout>
<net.cachapa.expandablelayout.ExpandableLayout
android:id="@+id/expandable_layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:el_duration="1000"
app:el_expanded="false"
app:el_parallax="0.5">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="some text"
android:textColor="#ffffff"
android:textSize="12sp"/>
</net.cachapa.expandablelayout.ExpandableLayout>
</LinearLayout>
【问题讨论】:
标签: java android android-studio android-layout expandable