如何使用滚动视图在Android中 [英] How to use Scroll View In Android
问题描述
我用两个滚动视图两种不同的布局。而这两个布局内的线性布局。
下面是我的XML文件。我不知道为什么滚动型
工作不适合我。
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =中心
机器人:方向=横向>
<的LinearLayout
机器人:ID =@ + ID / flightResultData
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =8DP
机器人:方向=垂直>
<的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
<的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:方向=横向>
<的LinearLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直>
< ImageView的
机器人:ID =@ + ID / onewayflightLogo
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =3DP
机器人:SRC =@可绘制/ SpiceJet航空公司/>
<的TextView
机器人:ID =@ + ID / onewayflightName
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=SpiceJet航空公司/>
<的TextView
机器人:ID =@ + ID / onewayflightNumber
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=9W - 496/>
< / LinearLayout中>
<的LinearLayout
机器人:layout_marginTop =8DP
机器人:layout_width =FILL_PARENT
机器人:layout_height =match_parent
机器人:方向=垂直>
<的TextView
机器人:ID =@ + ID / onewayflightTime
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =5DP
安卓的minlines =1
机器人:文本=06:00 - 7:05
机器人:TEXTSIZE =12dp/>
<的TextView
机器人:ID =@ + ID / onewayflightDuration
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =5DP
安卓的minlines =1
机器人:文本=1小时35米| NON STOP
机器人:TEXTSIZE =10dp/>
<的TextView
机器人:ID =@ + ID / onewayflightAmount
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =5DP
安卓的minlines =1
机器人:文本=RS 200000
机器人:TEXTSIZE =12dp/>
< / LinearLayout中>
< / LinearLayout中>
<查看
机器人:layout_width =1DP
机器人:layout_height =60dp
机器人:背景=@机器人:彩色/ darker_gray/>
<的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1
机器人:方向=横向>
<的LinearLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=垂直>
< ImageView的
机器人:ID =@ + ID / retrunflightLogo
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =3DP
机器人:SRC =@可绘制/ SpiceJet航空公司/>
<的TextView
机器人:ID =@ + ID / retrunflightName
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=SpiceJet航空公司/>
<的TextView
机器人:ID =@ + ID / retrunflightNumber
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文本=9W - 496/>
< / LinearLayout中>
<的LinearLayout
机器人:layout_marginTop =8DP
机器人:layout_width =FILL_PARENT
机器人:layout_height =match_parent
机器人:方向=垂直>
<的TextView
机器人:ID =@ + ID / retrunflightTime
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =5DP
安卓的minlines =1
机器人:文本=06:00 - 7:05
机器人:TEXTSIZE =12dp/>
<的TextView
机器人:ID =@ + ID / retrunflightDuration
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =5DP
安卓的minlines =1
机器人:文本=1小时35米| NON STOP
机器人:TEXTSIZE =10dp/>
<的TextView
机器人:ID =@ + ID / retrunflightAmount
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_marginLeft =5DP
安卓的minlines =1
机器人:文本=RS 200000
机器人:TEXTSIZE =12dp/>
< / LinearLayout中>
< / LinearLayout中>
< / LinearLayout中>
< / LinearLayout中>
< / LinearLayout中>
这是我的主要的布局中,我通过一个循环
动态充气我上面的XML <的LinearLayout
机器人:ID =@ + ID / flightResultData
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ + ID / sortFlightLayouts
机器人:layout_marginLeft =8DP
机器人:方向=垂直>
< / LinearLayout中>
和我的活动code我这样做:
无效setTestResultData(){
flightResult =(的LinearLayout)findViewById(R.id.flightResultData);
LinearLayout.LayoutParams flightDetailsLayout =新LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
LayoutInflater充气=(LayoutInflater)getBaseContext()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
LinearLayout.LayoutParams forUnderLine =新LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
forUnderLine.setMargins(0,0,0,0);
的for(int i = 0; I< 13;我++){
的LinearLayout flightInformations =(的LinearLayout)inflater.inflate(R.layout.flight_details_layout,NULL);
flightResult.addView(flightInformations);
}
}
我想你需要一个简单的,两列组织:
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:方向=横向>
<! - 东西放在这里应该出现上面的滚动区域的 - >
<滚动型
机器人:ID =@ + ID / left_side_scroller
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1>
<! - 左侧的内容放在这里 - >
< /滚动型>
<滚动型
机器人:ID =@ + ID / right_side_scroller
机器人:layout_width =0dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_weight =1>
<! - 右侧的内容放在这里 - >
< /滚动型>
<! - 东西放在这里应该出现了下面的滚动区域 - >
< / LinearLayout中>
另外(也许更好),它看起来像你应该使用两个的ListView
元素,而不是两个滚动型
元素。每个的ListView
将具有相同的布局参数如上所示的滚动型
。由于的ListView
管理着内部滚动,你再不需要滚动型
的。
另外,你可能想整个布局以填充屏幕,与过滤器和分类的元素总是在底部。为了达到这种效果,顶级的布局应该有安卓layout_height =FILL_PARENT
而不是WRAP_CONTENT
。另外,可滚动的区域应该有机器人:layout_height =0dp
和非零重量(他们已经这样做)
I have use two scroll views for two different layout. And those two layouts are inside a Linear Layout.
Here is my XML file. I don't why ScrollView
is not working for me
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/flightResultData"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/onewayflightLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:src="@drawable/spicejet" />
<TextView
android:id="@+id/onewayflightName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SpiceJet" />
<TextView
android:id="@+id/onewayflightNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9W - 496" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="8dp"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/onewayflightTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:minLines="1"
android:text="06:00 - 7:05"
android:textSize="12dp" />
<TextView
android:id="@+id/onewayflightDuration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:minLines="1"
android:text="1h 35m | Non Stop"
android:textSize="10dp" />
<TextView
android:id="@+id/onewayflightAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:minLines="1"
android:text="Rs 200000"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="1dp"
android:layout_height="60dp"
android:background="@android:color/darker_gray" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/retrunflightLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:src="@drawable/spicejet" />
<TextView
android:id="@+id/retrunflightName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SpiceJet" />
<TextView
android:id="@+id/retrunflightNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9W - 496" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="8dp"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/retrunflightTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:minLines="1"
android:text="06:00 - 7:05"
android:textSize="12dp" />
<TextView
android:id="@+id/retrunflightDuration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:minLines="1"
android:text="1h 35m |Non Stop"
android:textSize="10dp" />
<TextView
android:id="@+id/retrunflightAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:minLines="1"
android:text="Rs 200000"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
This is my main layout in which I am inflating my above xml dynamically through a loop
<LinearLayout
android:id="@+id/flightResultData"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/sortFlightLayouts"
android:layout_marginLeft="8dp"
android:orientation="vertical" >
</LinearLayout>
And in my activity code I am doing this:
void setTestResultData(){
flightResult=(LinearLayout)findViewById(R.id.flightResultData);
LinearLayout.LayoutParams flightDetailsLayout = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout.LayoutParams forUnderLine = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
forUnderLine.setMargins(0,0, 0, 0);
for(int i=0;i < 13;i++){
LinearLayout flightInformations=(LinearLayout)inflater.inflate(R.layout.flight_details_layout, null);
flightResult.addView(flightInformations);
}
}
I think you need a simple, two-column organization:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- stuff goes here that should appear above the scrolling areas -->
<ScrollView
android:id="@+id/left_side_scroller"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
<!-- contents of left side go here -->
</ScrollView>
<ScrollView
android:id="@+id/right_side_scroller"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" >
<!-- contents of right side go here -->
</ScrollView>
<!-- stuff goes here that should appear below the scrolling areas -->
</LinearLayout>
Alternatively (and perhaps better) it looks like you should be using two ListView
elements instead of two ScrollView
elements. Each ListView
would have the same layout parameters as shown above for the ScrollView
. Since a ListView
manages scrolling internally, you then don't need ScrollView
at all.
Also, you probably want the entire layout to fill the screen, with the "filter" and "sort" elements always at the bottom. To achieve this effect,the top-level layout should have android:layout_height="fill_parent"
instead of "wrap_content"
. Also, the scrollable areas should have android:layout_height="0dp"
and a non-zero weight (which they already do).
这篇关于如何使用滚动视图在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!