我有小问题。我在TextViews布局中使用scrollView,现在我必须设置按钮来向上滚动scrollView,但是我不知道如何在布局中设置好imageButton。这是我的架构。
我必须将这个imageButton设置在适当的位置,在架构中显示,TextView5太长了,当它开始滚动时,imageButton应该开始可见,而在滚动之前它应该是不可见的。
要向上滚动,我正在使用代码:
buttonScrollTop.setOnClickListener {
scrollView.fullScroll(ScrollView.FOCUS_UP)
}
我的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@color/colorBackgroundWhite">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<TextView
android:id="@+id/TextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:ellipsize="end"
android:gravity="center"
android:text="@string/text1" />
<TextView
android:id="@+id/TextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:ellipsize="end"
android:gravity="center"
android:paddingBottom="8dp"
android:text="@string/text2" />
<TextView
android:id="@+id/TextView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text3"/>
<TextView
android:id="@+id/TextView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center"
android:justificationMode="inter_word"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:text="@string/text4" />
<TextView
android:id="@+id/TextView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:justificationMode="inter_word"
android:ellipsize="end"
android:text="@string/bigstringtext5" />
<ImageButton
android:id="@+id/buttonScrollTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/ic_menu_upload" />
<TextView
android:id="@+id/TextView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:justificationMode="inter_word"
android:ellipsize="end"
android:gravity="center"
android:text="@string/text6" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
但是现在imageButton在TextView5下; /我不知道如何在架构中设置它。感谢您的帮助:)
您需要这种简单的方法:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@color/cardview_light_background">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<TextView
android:id="@+id/TextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:ellipsize="end"
android:gravity="center"
android:text="text1" />
<TextView
android:id="@+id/TextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:ellipsize="end"
android:gravity="center"
android:paddingBottom="8dp"
android:text="text2" />
<TextView
android:id="@+id/TextView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="text3"/>
<TextView
android:id="@+id/TextView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center"
android:justificationMode="inter_word"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:text="text4" />
<TextView
android:id="@+id/TextView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:justificationMode="inter_word"
android:ellipsize="end"
android:gravity="center"
android:text="text5" />
<TextView
android:id="@+id/TextView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:justificationMode="inter_word"
android:ellipsize="end"
android:gravity="center"
android:text="text6"
/>
<ImageButton
android:id="@+id/buttonScrollTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@android:drawable/ic_menu_upload" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
检查以下代码-如您在架构中定义的,它将在右上角显示图像按钮。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@color/colorBackgroundWhite">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<TextView
android:id="@+id/TextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:ellipsize="end"
android:gravity="center"
android:text="@string/text1" />
<TextView
android:id="@+id/TextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:ellipsize="end"
android:gravity="center"
android:paddingBottom="8dp"
android:text="@string/text2" />
<TextView
android:id="@+id/TextView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/text3"/>
<TextView
android:id="@+id/TextView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:gravity="center"
android:justificationMode="inter_word"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:text="@string/text4" />
<TextView
android:id="@+id/TextView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:justificationMode="inter_word"
android:ellipsize="end"
android:text="@string/bigstringtext5" />
<TextView
android:id="@+id/TextView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:justificationMode="inter_word"
android:ellipsize="end"
android:gravity="center"
android:text="@string/text6" />
</LinearLayout>
</ScrollView>
<ImageButton
android:contentDescription="@null"
android:id="@+id/buttonScrollTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="16dp"
app:srcCompat="@android:drawable/ic_menu_upload"
android:layout_alignParentRight="true" />
使滚动视图到达底部时使按钮可见
scrollView.getViewTreeObserver()
.addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if (scrollView.getChildAt(0).getBottom()
<= (scrollView.getHeight() + scrollView.getScrollY())) {
//scroll view is at bottom
//set the button visibility to visible here
} else {
//scroll view is not at bottom
//set the button visibility to gone here
}
}
});