当前位置:首页 » 安卓系统 » android横向滚动

android横向滚动

发布时间: 2023-04-30 15:03:04

1. 如何实现android中横向滚动的gridView

法1.直接用tablelayout gridview是根据你每行的单元数自动生成的行数;
法2.可以在代码里根据view数来动态设置列数,比如有10记录可以设置列数为10/3+1,这样就有三行四列了。
如果您对我的回答有不满意的地方,还请您继续追问;
答题不易,互相理解,互相帮助!

2. RecyclerView

RecyclerView 是Android一个更强大的控件,其不仅可以实现和ListView同样的效果,还有优化了ListView中的各种尺明不足。其可以实现数据纵向滚动,也可以实现横向滚动(ListView做不到横向滚动)。陵脊告接下来讲解RecyclerView的用法。

因为 RecyclerView 属于新增的控件,Android将RecyclerView定义在support库里。若要使用RecyclerView,第一步是要在 build.gradle 中添加对应的依赖库。

在 app/build.gradle 中的 dependencies闭包 添加以下内容:

然后点击顶部的Sync Now进行同步

由于 RecyclerView 不是内置在系统SDK中,需要把其野仔完整的包名路径写出来

创建ImageView来显示水果图片,TextView来显示水果名字。

为 RecyclerView 新增适配器 FruitAdapter ,并让其继承于 RecyclerView.Adapter ,把泛型指定为 FruitAdapter.ViewHolder 。

LayoutManager 用于指定RecyclerView的布局方式。 LinearLayoutManager 指的是线性布局。

运行效果:

把LinearLayout改成垂直排列,因为水果名字长度不一样,把宽度改为100dp。
ImageView和TextView都改为水平居中

通过调用 setOrientation() 把布局的排列方向改为水平排列。

得益于RecyclerView的设计,我们可以通过LayoutManager实现各种不同的排列方式的布局。

运行结果:

除了 LinearLayoutManager , RecyclerView 还提供了 GridLayoutManager(网格布局) 和 StaggeredGridLayoutManager(瀑布流布局)

GridLayoutManager(网格布局)

修改 MainActivity.java ,把

换成

GridLayoutManager (Context context, int spanCount)

运行结果:

StaggeredGridLayoutManager(瀑布流布局)

把LinearLayout的宽度设为 match_parent 是因为瀑布流的宽度是 根据布局的列数来自动适配的,而不是固定值 。(GridLayoutManager也是 根据布局的列数来自动适配的

StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3,StaggeredGridLayoutManager.VERTICAL);
StaggeredGridLayoutManager传入2个参数,第一个是布局的列数,第二个是布局的排列方向。

random.nextInt(20)+1 产生1-20的随机数
运行效果:

上图是GridLayoutManager,下图是StaggeredGridLayout。
当从显示效果来看,已经一目了然。
GridLayoutManager是会固定高度的,所以会留下很多空白区域。
相反,StaggeredGridLayout并不会固定高度,以至于就算子项的高度不一致,下一行的会自动靠拢上一行。

修改ViewHolder,添加fruitView变量来保存子项最外层布局的实例。

运行效果:

3. android scrollview的水平滚动条问题,哪位高手给指教下啊,谢谢了!!

如果你把内容包含在ScrollView中,当内容族稿超出高度时会自动出现滚动条。

另外,使用控件HorizontalScrollView 来包住你的内容时,兆宽孝
如果你的内容假设是一个LinearLayout, 那么当LinearLayout的宽度超过屏幕时, 将会自动产生滚动条,当你拖动鼠标时,效果跟scrollView一样,不过是横向而己

例:
纵向滚动
<ScrollView>
<LinearLayout ........>
<TextView ...../>
<TextView ...../>
<TextView ...../>
<TextView ...../>
</LineraLayout>
</ScrollView>

模向滚动
<HorizontalScrollView >
<LinearLayout ........>
<TextView ...../>
<TextView ...../>
<TextView ...../>
<TextView ...../巧宽>
</LineraLayout>
</HorizontalScrollView >

有时候甚至可以做到横向纵向都支持,只需要你合理设计就可以, 注意ScrollView中只能加一个控制,不能超过两个

4. android 手势判断是横向滑动还是纵向 csdn

对于Android中的手势识别可以从以下三个Listener入手——OnTouchListener、OnGestureListener、OnDoubleTapListener。这三个监听器分别是触摸监听、手势滑动监听和屏幕双击操作监听。很多的时候我们需要这些手势识别的操作,例如我们自定义控件的时候就经常会用到。下面就对这三个监听器分别进行介绍。

触摸监听器OnTouchListener
让我们的Activity去现实此接口,并重写onTouch方法。重写OnTouchListener的onTouch方法 此方法在触摸屏被触摸,即发生触摸事件(接触和抚摸两个事件)的时候被调用。示范代码如下:

@Override
public boolean onTouch(View v, MotionEvent event) {
detector.onTouchEvent(event);
Toast.makeText(this, "onTouch", TIME_OUT).show();
return true;
}

手势滑动监听器OnGestureListener
让我们的Activity去现实此接口,并重写onFling、onLongPress、onScroll、onDown、onShowPress、onSingleTapUp方法。示范代码如下:

/**
* 手势滑动时别调用
*/
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {

if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE) {
Toast.makeText(this, "向左滑动", TIME_OUT).show();
} else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE) {
Toast.makeText(this, "向右滑动", TIME_OUT).show();
}
return false;
}

/**
* 长按时被调用
*/
@Override
public void onLongPress(MotionEvent e) {
Toast.makeText(this, "触发长按回调", TIME_OUT).show();
}

/**
* 滚动时调用
*/
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Toast.makeText(this, "触发滚动回调", TIME_OUT).show();
return false;
}

/**
* 在按下动作时被调用
*/
@Override
public boolean onDown(MotionEvent e) {
Toast.makeText(this, "按下回调", TIME_OUT).show();
return false;
}

/**
* 按住时被调用
*/
@Override
public void onShowPress(MotionEvent e) {
Toast.makeText(this, "按住不松回调", TIME_OUT).show();
}

/**
* 抬起时被调用
*/
@Override
public boolean onSingleTapUp(MotionEvent e) {
Toast.makeText(this, "触发抬起回调", TIME_OUT).show();
return false;
}

双击屏幕监听器OnDoubleTapListener
让我们的Activity去现实此接口,并重写onDoubleTap、onDoubleTapEvent、onSingleTapConfirmed方法。示范代码如下:

@Override
public boolean onDoubleTap(MotionEvent arg0) {
Toast.makeText(this, "触发双击回调", TIME_OUT).show();
return false;
}

@Override
public boolean onDoubleTapEvent(MotionEvent arg0) {
Toast.makeText(this, "触发双击的按下跟抬起回调", TIME_OUT).show();
return false;
}

@Override
public boolean onSingleTapConfirmed(MotionEvent arg0) {
Toast.makeText(this, "触发单击确认回调", TIME_OUT).show();
return false;
}

5. android文字横向滚动的自定义view

TextView实现文字滚动需要以下几个要点: 1.文字长度长于可显示范围:android:singleLine=”true” 2.设置可滚到,或显示样式
TextView实现文字滚动需要以下几个要点:
1.文字长度长于可显示范围:android:singleLine=”true”
2.设置可滚到,或显示样式:android:ellipsize=”marquee”
3.TextView只有在获取焦点后才会滚动显示隐藏文字,因此需要在包中新建一个类,继承TextView。重写isFocused方法,这个方法默认行为是,如果TextView获得焦点,方法返回true,失去焦点则返回false。跑马灯效果估计也是用这个方法判断是否获得焦点,所以把它的返回值始终设置为true。
自定义一个
AlwaysMarqueeTextView 类
public class AlwaysMarqueeTextView extends TextView { public AlwaysMarqueeTextView(Context context) { super(context); } public AlwaysMarqueeTextView(Context context, AttributeSet attrs) { super(context, attrs); } public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean isFocused() { return true; }
在布局XML文件中加入这么一个AlwaysMarqueeTextView,这个加入方法也是刚刚学的。
<com.examples.AlwaysMarqueeTextView android:id=“@+id/AMTV1″ android:layout_width=“fill_parent” android:layout_height=“wrap_content” android:lines=“1″ android:focusable=“true” android:focusableInTouchMode=“true” android:scrollHorizontally=“true” android:marqueeRepeatLimit=“marquee_forever” android:ellipsize=“marquee” android:background=“@android:color/transparent” />
ellipsize属性
设置当文字过长时,该控件该如何显示。有如下值设置:”start”—–省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动)
marqueeRepeatLimit属性
在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。
focusable属性
自己猜测的,应该是能否获得焦点,同样focusableInTouchMode应该是滑动时能否获得焦点。
组合View的问题:
< LinearLayout xmlns:android =“http://schemas.android.com/apk/res/android” android:orientation =“vertical” android:gravity =“center_vertical” android:background =“@drawable/f_background” android:layout_width =“fill_parent” android:focusable =“true” android:layout_height =“50px” > < TextView android:id =“@+id/info_text” android:focusable =“true” android:layout_width =“fill_parent” android:layout_height =“wrap_content” android:text =“test marquee .. “ android:textColor =“@color/black” android:singleLine =“true” android:ellipsize =“marquee” android:marqueeRepeatLimit =“3″ android:textSize =“18sp” /> < TextView android:id =“@+id/date_text” android:layout_width =“fill_parent” android:layout_height =“wrap_content” android:layout_gravity =“bottom” android:textColor =“@color/gray” android:text =“2010/05/28″ android:textSize =“12sp” /> </ LinearLayout >
上面示例中2个TextView组合为一个View,由于设置了LinearLayout为focusable而TextView就没法取得焦点了,这样 这个TextView的跑马灯效果就显示不出来,就算你也设置TextView的

6. android TextView文本动画横向移动时间

TextView实现文字滚动需要以下几个要点:
1.文字长度长于可显示范围:android:singleLine="true"
2.设置可滚到,或显示样式:android:ellipsize="marquee"
3.TextView只有在获取焦点后才会滚动显示隐藏文字,因此需要在包中新建一个类,继承TextView。重写isFocused方法,这个方法默认行为是,如果TextView获得焦点,方法返回true,失去焦点则返回false。跑马灯效果估计也是用这个方法判断是否获得焦点,所以把它的返回值始终设置为true。

Java语言: AlwaysMarqueeTextView 类
public class AlwaysMarqueeTextView extends TextView {
public AlwaysMarqueeTextView(Context context) {
super(context);
}
public AlwaysMarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public AlwaysMarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean isFocused() {
return true;
}
在布局XML文件中加入这么一个AlwaysMarqueeTextView,这个加入方法也是刚刚学的。
XML语言: layout.xml
<com.examples.AlwaysMarqueeTextView
android:id=“@+id/AMTV1″
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:lines=“1″
android:focusable=“true”
android:focusableInTouchMode=“true”
android:scrollHorizontally=“true”
android:marqueeRepeatLimit=“marquee_forever”
android:ellipsize=“marquee”
android:background=“@android:color/transparent”
/>
ellipsize属性
设置当文字过长时,该控件该如何显示。有如下值设置:”start”—–省略号显示在开头;”end”——省略号显示在结尾;”middle”—-省略号显示在中间;”marquee” ——以跑马灯的方式显示(动画横向移动)
marqueeRepeatLimit属性
在ellipsize指定marquee的情况下,设置重复滚动的次数,当设置为marquee_forever时表示无限次。
组合View的问题:
XML语言: 组合View
< LinearLayout
xmlns:android =“http://schemas.android.com/apk/res/android”
android:orientation =“vertical”
android:gravity =“center_vertical”
android:background =“@drawable/f_background”
android:layout_width =“fill_parent”
android:focusable =“true”
android:layout_height =“50px” >
< TextView
android:id =“@+id/info_text”
android:focusable =“true”
android:layout_width =“fill_parent”
android:layout_height =“wrap_content”
android:text =“test marquee .. “
android:textColor =“@color/black”
android:singleLine =“true”
android:ellipsize =“marquee”
android:marqueeRepeatLimit =“3″
android:textSize =“18sp”
/>
< TextView
android:id =“@+id/date_text”
android:layout_width =“fill_parent”
android:layout_height =“wrap_content”
android:layout_gravity =“bottom”
android:textColor =“@color/gray”
android:text =“2010/05/28″
android:textSize =“12sp”
/>
</ LinearLayout >
上面示例中2个TextView组合为一个View,由于设置了LinearLayout为focusable而TextView就没法取得焦点了,这样 这个TextView的跑马灯效果就显示不出来,就算你也设置TextView的 android:focusable="true" 也是 没用的. 这个时候就要使用addStatesFromChildren 这个属性了,在LinearLayout中设置这个属性,然后设置TextView的focusable= "true" 就可以了.关于 addStatesFromChildren的说明:
Sets whether this ViewGroup's drawable states also include its children's drawable states.

7. android 如何实现listview可同时纵向,横向滚动

gallery 里面套入 多个 自定义 view 然后里面 放ListView~

8. Android中TextView如何实现水平和垂直滚动

殇 殇云的专栏 云的专栏 软件开发锋颤 软件开发 一 一、只想让TextView显示一行,但是文字超过 、只想让TextView显示一行,但是文字超过 在开头显示省略号 android:singleLine="true" android:ellipsize="start" 在结尾显示省略号 android:singleLine="true" android:ellipsize="end" 在中间显示省略号 android:singleLine="true" android:ellipsize="middle" 横向自动滚动(跑马灯效果)段裂 android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:focusable="true" android:focusableInTouchMode="true" 以上4个效果都要加上�0�2android:singleLine="true",因为TextView默认是会自动换行的 android:marqueeRepeatLimit="marquee_forever"是设置银燃败永远重复,当然你也可以设置具体的数字 android:focusable="true"和android:focusableInTouchMode="true"一定要加上,不然滚动效果出不来在Java代码中加入下面一句话就可以实现垂直滚动

9. android实现上下滑动

布局最外包一层滚动条

<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
强制横竖屏
在配置文件中对Activity节点添加android:screenOrientation属性(landscape是横向,portrait是纵向)

10. androidstudio怎么进行方法重写的快捷键

Android Studio常用快捷键 1. Ctrl+D: 集合了复制和粘贴两个操作,如果有选中的部分就复制选中的部分,并在选中部分的后面 粘贴出来,如果没有选中的部分,就复制光标所在的行,并在此行的下面粘贴出来。 2. Ctrl+空格: 输入代码时按此组合键会列出与之相匹配的类、方法名、成员变量等,起智能提示的作用。 在编辑XML文件一样有用。 3. Ctrl+向下箭头 或Ctrl+向上箭头:在有自动匹配下拉列表时,此快捷键会自动关掉下拉列表, 光标移动到下/上一行。 4. 自动匹配下拉列表的排列方式的切换:在自动匹配下拉列表的右下角有个“π”图标,点击后可选 是按:实用性、字母两种排列方式。 5. Ctrl+斜杠 、Ctrl+shift+斜杠: Ctrl+斜杠:注释或取消注释当前行或选中的代码块,以双斜杠的方式即“//” Ctrl+shift+斜杠:注释或取消注释选中的代码块,以“/*……*/”方式注释, 6. Ctrl+shift+Enter:自动匹配相对应的语法结构,比如if,do-while,try-catch等结构。 7. Ctrl+F:搜索 8. Ctrl+句点:在自动匹配下拉列表中,选中第一个item 9. 感叹号:在自动匹配下拉列表中,上下键选中一个返回结果为boolean的item,按感叹号会自动取反: 10. Ctrl+Enter :在自动匹配下拉列表中,在没有选中的item时,默认选中第一个item。 11. Ctrl+shift+A:快速查找android studio中的菜单。 12. Ctrl+N:快速查找类名、文件 13. Ctrl+B:直接跳转到类、方法、成员变量定义的地方。与Ctrl+鼠标左键效果一样 14. Ctrl+Alt+B:查询有哪些类实现了光标所在的接口。 15. Ctrl+Alt+shift+I:检测代码,比如检测一些定义了,但没有使用过的变量或方法。检测的目的是为了提高代码效率。 16.Ctrl+Alt+shift+N :快速打开输入的方法或变量。 17.Ctrl+shift+F7:以高亮的形式标志处一些相关的东西,这里主要由三种情况:1.光标的位置在implement时, 会把类实现了接口的方法名给标志出来;2.光标的位置在return时,会标志出方法的所有退出的地方; 3.光标在try或者throws关键字处时,会标志出会产生异常的语句。 18.shift+鼠标滚动:可实现编辑界面的横向滚动。 19.Ctrl+Alt+V:调用方法时传入的参数是比较复杂的表达式时,可用此组合键重构变量,以简化代码的复杂度。 在组合键之前要先选中表达式。 20. Ctrl+D:比较两个jar文件,在同一工程中,选中两个jar文件,按此组合键 21.Ctrl+O:子类想重写父类的方法时,按此组合键可显示所有父类的方法。接口对应的组合键时Ctrl+I。 22.Ctrl+shift+I:快速查看方法体,想查看一个方法是如何实现的,可把光标移至方法处,按此组合键。 23.Ctrl+Q:把光标移至方法处,按此组合键可快速查看方法的说明文档。 24.Ctrl+~:切换编辑界面的风格,快捷键设计 25.Alt+shift+C:查看工程的最近修改。 26.Ctrl+E:快速查看最近打开打开过的文件。 27.shift+F6:可对类、方法、变量进行重命名,使用的地方会自动更新名字。 28.Alt+F1:快速打开project view、structure view 等查看相应的元素。 29.Ctrl+Alt+F7:查看一个类、方法、成员变量在整个工程中的使用情况。 30.Ctrl+shift+空格:在赋值或者是在方法中要传入参数时,列出类型匹配的方法名、成员变量名。 31.Ctrl+Alt+T:选中一块代码,按此组合键,可快速添加if 、for、try/catch等语句。 32.Ctrl+Tab:打开界面切换窗口,保持按住Ctrl键,选中相应的要打开的窗口。 33.Ctrl+W:选中光标所在的所在的单词(一个成员变量或者是一个方法名),多按一次会选中所在的语句, 再多按一次会选中所在的代码块。。。依次类推,每增加一次会扩大一级选中的范围。

热点内容
哪些电脑配置低 发布:2025-05-20 09:34:16 浏览:954
地板网站源码 发布:2025-05-20 09:27:23 浏览:346
安卓视频转换器怎么使用 发布:2025-05-20 09:20:52 浏览:544
telnet批量脚本 发布:2025-05-20 09:11:58 浏览:627
搭建jrebel服务器 发布:2025-05-20 08:57:40 浏览:902
安卓手机上网怎么连接电脑 发布:2025-05-20 08:28:30 浏览:548
福建公积金密码是什么 发布:2025-05-20 08:28:13 浏览:507
学习编程用什么软件好 发布:2025-05-20 08:27:28 浏览:599
我的世界电脑版服务器小游戏怎么下载 发布:2025-05-20 08:17:12 浏览:533
离线语音识别android 发布:2025-05-20 08:11:37 浏览:153