Android 给Edittext设置滚动条
时间: 2024-05-29 10:08:43 浏览: 10
可以通过在布局文件或代码中设置属性来给Edittext设置滚动条,例如:
在布局文件中:
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
在代码中:
EditText editText = findViewById(R.id.editText);
editText.setVerticalScrollBarEnabled(true);
相关问题
android输入框java设置可滚动
要设置Android输入框(java)可滚动,您需要将输入框包装在一个可滚动的布局中。以下是示例代码:
XML布局文件:
```xml
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:scrollbars="vertical"/>
</ScrollView>
```
Java代码:
```java
EditText editText = findViewById(R.id.editText);
editText.setScroller(new Scroller(context));
editText.setVerticalScrollBarEnabled(true);
editText.setMovementMethod(new ScrollingMovementMethod());
```
此代码将在垂直方向上使输入框可滚动,并添加了滚动条。您还可以添加其他滚动条选项,例如水平滚动条,根据需要进行调整。
android ScrollView 嵌套 EditText 后,EditText中的文本如何滑动
如果你在一个 ScrollView 中嵌套了一个 EditText,而且 EditText 中的文本超出了屏幕范围,那么你可以通过以下两种方法使其可以滚动:
1. 设置 EditText 的属性
在 EditText 中添加以下属性可以使其可以滚动:
```
android:scrollbars="vertical"
android:scrollbarStyle="insideOverlay"
```
其中 `android:scrollbars="vertical"` 表示只显示垂直滚动条,`android:scrollbarStyle="insideOverlay"` 表示滚动条在内容之上,不会占用内容的位置。
2. 使用 NestedScrollView
NestedScrollView 是一个可以嵌套滚动的 ScrollView,可以将 EditText 放入其中。使用方法如下:
```
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarStyle="insideOverlay" />
</androidx.core.widget.NestedScrollView>
```
这样就可以将 EditText 放入 NestedScrollView 中,使其可以滚动。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)