ViewStub HorizontalScrollView

ViewStub组件和<include>标签的作用类似,主要是为了提高布局的重用性,及布局的模块化。它们之间最大的差别是,

  • ViewStub中的布局不会随着它所在布局的渲染而渲染,ViewStub中的布局只有在需要的时候才会渲染到主界面中
  • 而<include>标签中的布局会随着它所在布局的渲染而渲染

include、merge、viewstub的作用

滑屏分页,在iphone中由UIScrollView实现,而android的滑动屏幕由HorizontalScrollView和ScrollView实现,HorizontalScrollView支持横向滑屏,ScrollView支持竖向滑屏,将两者嵌套使用就可以达到iphone的UIScrollView效果:

<ScrollView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:layout_alignParentBottom=”true”
android:layout_alignParentTop=”true”
android:layout_marginBottom=”50dp”
android:scrollbars=”vertical”
>

<HorizontalScrollView
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:orientation=”horizontal”
android:layout_gravity=”center_horizontal” >

<LinearLayout
android:id=”@+id/allnum”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:gravity=”center”
android:layout_gravity=”center”
android:orientation=”vertical” >
</LinearLayout>
</HorizontalScrollView>
</ScrollView>

HorizontalScrollView是一种FrameLayout(框架布局),其子项被滚动查看时是整体移动的,并且子项本身可以是一个有复杂层次结构的布局管理器。一个常见的应用是子项在水平方向中,用户可以滚动显示顶层水平排列的子项(items)。

HorizontalScrollView不可以和ListView同时用,因为ListView有自己的滚动条设置。最重要的是,如果在需要显示很大的list的情况下,两者同时用则会使ListView在一些重要的优化上失效。出现这种失效的原因在于,HorizontalScrollView会*ListView用HorizontalScrollView本身提供的空间容器(infinite container)来显示完整的列表。
类似的情况,TextView也有自己的滚动条,所以不需要ScrollView。但这两者是可以同时使用的,使用的结果会是在一个更大的容器里显示文本视图。
用HorizontalScrollView包装TableLayout,这样,当内容很长时,就会出现横向滚动条

<HorizontalScrollView
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”>
<TableLayout
android:id=”@+id/tl_forcast_result”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:padding=”10dp”>
</TableLayout>
</HorizontalScrollView>

但此时又出现了另一个问题,加上HorizontalScrollView后,虽然我已经设了TableLayout的宽度是fill_parent。但当内容较少时,TableLayout还是根据内容自适应宽度,不能满屏。 此时,需要设置一个属性就能解决问题了。设置HorizontalScrollView的android:fillViewport=”true”。也就是设置是否将HorizontalScrollView的内容宽度拉伸以适应视口(viewport)

关于分割线: 
我们可以通过在两个TableRow之间添加一个<View>,并设置它的layout_height=”1dip”和background来解决。但这样就多产生了一个View,有点浪费
其实还可以设置TableRow的bottomMargin=1来产生一条分割线。

Gallery widget (adapter-based) is not the best starting point for implementing something like this:

  • It supports padding around each gallery item. Gallery items don’t generally take up all the horizontal space, several can be visible at the same time, next to each other.
  • When user swipes, Gallery flings, potentially over several child elements. Preventing that requires some hacky code.
  • It is adapter-based. Cannot define all the views in singe XML layout, need to create/inflate them in adapter.

The most prominent feature that Gallery has andHorizontalScrollView lacks, is that gallery always tries to center on one of its elements. HorizontalScrollView can be scrolled freely, and will happily stay at any horizontal scroll offset. But this is not hard to fix. By listening in on touch events and detecting flings, I can make it smooth-scroll to the nearest screen-ful in fling’s direction. I’m saying “screen-ful” not “element” because HorizontalScrollView is a subclass of FrameLayout and can only have a single child view. So I’m putting a horizontal LinearLayout in there, and the child views go in that layout. The LinearLayout is slightly hacked too to set appropriate widths for its child views.

Dynamically add to horizontalscrollview

  • the problem is, it creates main horizontalScrollView first, so it goes onMeasure method and calculates mMaxTabWidth. Then it starts to add these customTabView items as much as my tab count. And then it kicks onMeasure of horizontalScrollView again… Here it crashes!
  • HorizontalScrollView scroll = new HorizontalScrollView(this);
    scroll.setFillViewport(true); // if we set it to false, then HorizontalScrollView height measures correctly
  • 设置HorizontalScrollView的android:fillViewport=”true”。也就是设置是否将HorizontalScrollView的内容宽度拉伸以适应视口(viewport)

Your HorizontalScrollView must contain a container like a LinearLayout or a RelativeLayout, grab an instance to that Layout in your activity, and add the views as required…

LinearLayout yourLayout = (LinearLayout)findViewById(R.id.someID);

and then iterate through the number of items in your database and keep adding the views to your Layout until end like this…

for (int i = 0; i < yourData.size(); i++) {             
  TextView tv = new TextView(getApplicationContext());
  tv.setText(yourData.get(i));
  yourLayout.addView(tv);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

生活在西班牙

自己动手丰衣足食

BlueAsteroid

Just another WordPress.com site

Jing's Blog

Just another WordPress.com site

Start from here......

我的心情魔方

天才遠私廚

希望能做一個分享各種資訊的好地方

语义噪声

西瓜大丸子汤的博客

笑对人生,傲立寰宇

Just another WordPress.com site

Where On Earth Is Waldo?

A Project By Melanie Coles

the Serious Computer Vision Blog

A blog about computer vision and serious stuff

Cauthy's Blog

paper review...

Cornell Computer Vision Seminar Blog

Blog for CS 7670 - Special Topics in Computer Vision

datarazzi

Life through nerd-colored glasses

Luciana Haill

Brainwaves Augmenting Consciousness

槑烎

1,2,∞

Dr Paul Tennent

and the university of nottingham

turn off the lights, please

A bunch of random, thinned and stateless thoughts around the Web

%d bloggers like this: