滚动视图中的相对布局不起作用

发布于 2024-12-11 21:29:08 字数 3801 浏览 0 评论 0原文

我正在尝试将滚动视图置于相对布局中。这里,上部和下部的布局的某些部分是恒定的。所以我在里面使用了另一种布局,其中包含需要滚动的项目。

但这根本没有发生,而且屏幕看起来也很糟糕。我在定义滚动视图的部分底部出现一些黑色空间。

这是我的 xml,

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView android:id="@+id/textView1" android:layout_below="@+id/logoTop"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:text="TextView" android:layout_alignLeft="@+id/logoTop"></TextView>

<ImageView android:src="@drawable/logotop" android:id="@+id/logoTop"
    android:layout_alignParentTop="true" android:layout_width="match_parent"  
    android:scaleType="fitXY" android:layout_height="wrap_content"></ImageView>

<ImageView android:src="@drawable/bardown" android:id="@+id/barDown"
    android:layout_width="match_parent" android:scaleType="fitXY"
    android:layout_alignParentBottom="true" android:layout_height="40dip">
</ImageView>

<ImageButton 
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:id="@+id/btnMap" android:layout_centerInParent="true" android:src="@drawable/map"
    android:background="@drawable/mapselector" android:scaleType="fitXY"
    android:layout_alignParentBottom="true">
</ImageButton>

<ImageButton 
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:id="@+id/utBtn" android:background="@drawable/utilitiesicon"
    android:scaleType="fitXY" android:layout_alignTop="@+id/barDown" android:src="@drawable/utilityselector"
    android:layout_alignParentRight="true" android:layout_marginRight="10dip"
    android:layout_marginTop="3dip">
</ImageButton>

 <ScrollView 
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_below="@+id/logoTop"
  android:layout_above="@+id/barDown"
  android:scrollbars="none">

    <RelativeLayout  android:id="@+id/tour" 
    android:layout_width="wrap_content" android:layout_height="wrap_content">

<ImageView android:id="@+id/abtHome" android:layout_below="@+id/logoTop"
    android:src="@drawable/abouthome" android:scaleType="fitXY"
    android:layout_width="match_parent" android:maxWidth="230dip"
    android:layout_height="230dip">
</ImageView> 

<ListView android:id="@+id/tourList" android:layout_below="@+id/abtHome"
    android:scrollbars="none" android:visibility="visible"
    android:layout_centerInParent="true" android:layout_above="@+id/barDown"
    android:background="#FFFFFF" android:cacheColorHint="#00000000" android:fadingEdge="horizontal"
    android:fastScrollEnabled="true" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:isScrollContainer="false">
</ListView> 

<TextView android:id="@+id/tourcity" android:layout_above="@+id/tourList"
    android:layout_height="wrap_content" android:layout_width="match_parent"
    android:textSize="25dip" android:layout_centerHorizontal="true"
    android:editable="false" android:paddingTop="5dip" android:textColor="@color/white"
    android:paddingBottom="5dip" android:background="#80000000" android:gravity="center"></TextView>

</RelativeLayout>
</ScrollView>

<ImageButton android:src="@drawable/arrowselector"
    android:layout_height="wrap_content" android:layout_width="wrap_content"   android:layout_alignTop="@+id/barDown"
    android:id="@+id/btnBack" android:background="@drawable/arrowleft" android:layout_marginLeft="10dip"
     android:layout_marginTop="6dip"></ImageButton>

   </RelativeLayout>

有什么答案吗?

I am trying to put a scroll view to relative layout. Here some part of the layout on the upper and bottom part are to be constant. So I'm using one more layout inside,which has items that need to be scrolled.

But this is not at all happening, also the screen looks very bad with this. I am getting some black space in the bottom of the part where I defined scroll view.

Here is my xml,

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView android:id="@+id/textView1" android:layout_below="@+id/logoTop"
    android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:text="TextView" android:layout_alignLeft="@+id/logoTop"></TextView>

<ImageView android:src="@drawable/logotop" android:id="@+id/logoTop"
    android:layout_alignParentTop="true" android:layout_width="match_parent"  
    android:scaleType="fitXY" android:layout_height="wrap_content"></ImageView>

<ImageView android:src="@drawable/bardown" android:id="@+id/barDown"
    android:layout_width="match_parent" android:scaleType="fitXY"
    android:layout_alignParentBottom="true" android:layout_height="40dip">
</ImageView>

<ImageButton 
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:id="@+id/btnMap" android:layout_centerInParent="true" android:src="@drawable/map"
    android:background="@drawable/mapselector" android:scaleType="fitXY"
    android:layout_alignParentBottom="true">
</ImageButton>

<ImageButton 
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:id="@+id/utBtn" android:background="@drawable/utilitiesicon"
    android:scaleType="fitXY" android:layout_alignTop="@+id/barDown" android:src="@drawable/utilityselector"
    android:layout_alignParentRight="true" android:layout_marginRight="10dip"
    android:layout_marginTop="3dip">
</ImageButton>

 <ScrollView 
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:layout_below="@+id/logoTop"
  android:layout_above="@+id/barDown"
  android:scrollbars="none">

    <RelativeLayout  android:id="@+id/tour" 
    android:layout_width="wrap_content" android:layout_height="wrap_content">

<ImageView android:id="@+id/abtHome" android:layout_below="@+id/logoTop"
    android:src="@drawable/abouthome" android:scaleType="fitXY"
    android:layout_width="match_parent" android:maxWidth="230dip"
    android:layout_height="230dip">
</ImageView> 

<ListView android:id="@+id/tourList" android:layout_below="@+id/abtHome"
    android:scrollbars="none" android:visibility="visible"
    android:layout_centerInParent="true" android:layout_above="@+id/barDown"
    android:background="#FFFFFF" android:cacheColorHint="#00000000" android:fadingEdge="horizontal"
    android:fastScrollEnabled="true" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:isScrollContainer="false">
</ListView> 

<TextView android:id="@+id/tourcity" android:layout_above="@+id/tourList"
    android:layout_height="wrap_content" android:layout_width="match_parent"
    android:textSize="25dip" android:layout_centerHorizontal="true"
    android:editable="false" android:paddingTop="5dip" android:textColor="@color/white"
    android:paddingBottom="5dip" android:background="#80000000" android:gravity="center"></TextView>

</RelativeLayout>
</ScrollView>

<ImageButton android:src="@drawable/arrowselector"
    android:layout_height="wrap_content" android:layout_width="wrap_content"   android:layout_alignTop="@+id/barDown"
    android:id="@+id/btnBack" android:background="@drawable/arrowleft" android:layout_marginLeft="10dip"
     android:layout_marginTop="6dip"></ImageButton>

   </RelativeLayout>

Any answer for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

眼泪淡了忧伤 2024-12-18 21:29:08

将scrollView高度设置为wrap_content或fill_parent不会有太大区别。
尝试

android:fillViewport="true"

Setting scrollView height to wrap_content or fill_parent wouldn't make much difference.
try

android:fillViewport="true"
狠疯拽 2024-12-18 21:29:08

从您对问题的描述来看,您的 ScrollView 高度似乎应该是 fillParent

From the description you have given to your problem, it seems that your ScrollView height should be fillParent

浮生未歇 2024-12-18 21:29:08

我认为出现问题是因为试图在 ScrollView 中包含 ListView ,所以这里有一个没有 ScrollView 的解决方案:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/logoTop"
        android:layout_below="@+id/logoTop"
        android:text="TextView" >
    </TextView>

    <ImageView
        android:id="@+id/logoTop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:scaleType="fitXY"
        android:src="@drawable/logotop" >
    </ImageView>

    <ImageView
        android:id="@+id/barDown"
        android:layout_width="match_parent"
        android:layout_height="40dip"
        android:layout_alignParentBottom="true"
        android:scaleType="fitXY"
        android:src="@drawable/bardown" >
    </ImageView>

    <ImageButton
        android:id="@+id/btnMap"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:background="@drawable/mapselector"
        android:scaleType="fitXY"
        android:src="@drawable/map" >
    </ImageButton>

    <ImageView
        android:id="@+id/abtHome"
        android:layout_width="match_parent"
        android:layout_height="230dip"
        android:layout_below="@+id/logoTop"
        android:maxWidth="230dip"
        android:scaleType="fitXY"
        android:src="@drawable/abouthome" >
    </ImageView>

    <ImageButton
        android:id="@+id/utBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/barDown"
        android:layout_marginRight="10dip"
        android:layout_marginTop="3dip"
        android:background="@drawable/utilitiesicon"
        android:scaleType="fitXY"
        android:src="@drawable/utilityselector" >
    </ImageButton>

    <TextView
        android:id="@+id/tourcity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tourList"
        android:layout_centerHorizontal="true"
        android:background="#80000000"
        android:editable="false"
        android:gravity="center"
        android:paddingBottom="5dip"
        android:paddingTop="5dip"
        android:textColor="@color/white"
        android:textSize="25dip" >
    </TextView>

    <ListView
        android:id="@+id/tourList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/barDown"
        android:layout_below="@+id/abtHome"
        android:layout_centerInParent="true"
        android:background="#FFFFFF"
        android:cacheColorHint="#00000000"
        android:fadingEdge="horizontal"
        android:fastScrollEnabled="true"
        android:isScrollContainer="false"
        android:scrollbars="none"
        android:visibility="visible" >
    </ListView>

    <ImageButton
        android:id="@+id/btnBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/barDown"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="6dip"
        android:background="@drawable/arrowleft"
        android:src="@drawable/arrowselector" >
    </ImageButton>

</RelativeLayout>

因为我没有所有资源,所以我不能确实看到了它的外观,但我想您可以根据需要调整视图的布局设置。

I think the problem occur because trying to include ListView in ScrollView, so here is a solution without the ScrollView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/logoTop"
        android:layout_below="@+id/logoTop"
        android:text="TextView" >
    </TextView>

    <ImageView
        android:id="@+id/logoTop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:scaleType="fitXY"
        android:src="@drawable/logotop" >
    </ImageView>

    <ImageView
        android:id="@+id/barDown"
        android:layout_width="match_parent"
        android:layout_height="40dip"
        android:layout_alignParentBottom="true"
        android:scaleType="fitXY"
        android:src="@drawable/bardown" >
    </ImageView>

    <ImageButton
        android:id="@+id/btnMap"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:background="@drawable/mapselector"
        android:scaleType="fitXY"
        android:src="@drawable/map" >
    </ImageButton>

    <ImageView
        android:id="@+id/abtHome"
        android:layout_width="match_parent"
        android:layout_height="230dip"
        android:layout_below="@+id/logoTop"
        android:maxWidth="230dip"
        android:scaleType="fitXY"
        android:src="@drawable/abouthome" >
    </ImageView>

    <ImageButton
        android:id="@+id/utBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/barDown"
        android:layout_marginRight="10dip"
        android:layout_marginTop="3dip"
        android:background="@drawable/utilitiesicon"
        android:scaleType="fitXY"
        android:src="@drawable/utilityselector" >
    </ImageButton>

    <TextView
        android:id="@+id/tourcity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/tourList"
        android:layout_centerHorizontal="true"
        android:background="#80000000"
        android:editable="false"
        android:gravity="center"
        android:paddingBottom="5dip"
        android:paddingTop="5dip"
        android:textColor="@color/white"
        android:textSize="25dip" >
    </TextView>

    <ListView
        android:id="@+id/tourList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/barDown"
        android:layout_below="@+id/abtHome"
        android:layout_centerInParent="true"
        android:background="#FFFFFF"
        android:cacheColorHint="#00000000"
        android:fadingEdge="horizontal"
        android:fastScrollEnabled="true"
        android:isScrollContainer="false"
        android:scrollbars="none"
        android:visibility="visible" >
    </ListView>

    <ImageButton
        android:id="@+id/btnBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/barDown"
        android:layout_marginLeft="10dip"
        android:layout_marginTop="6dip"
        android:background="@drawable/arrowleft"
        android:src="@drawable/arrowselector" >
    </ImageButton>

</RelativeLayout>

Because I don't have all the resources, I can't really see how it looks, but I suppose you can adjust some more the layout settings of the views as you wish.

甜尕妞 2024-12-18 21:29:08

你的布局很混乱...
至少,我可以区分以下观察结果:

  • 使用了冲突的标签。
    例如在“btnMap”元素中:

    android:layout_alignParentBottom="true"
    android:layout_centerInParent="true"
    
  • 内部层的元素相对于高级层的元素对齐。
    例如,位于 ScrollView 内部的“abtHome”与位于上层(与 ScrollView 相同)的“logoTop”对齐。
  • ListView是在ScrollView内部使用的。如果要添加与 ListView 项目不同的 ListView 视图的顶部或底部,但必须与 ListView 的项目一起滚动,则必须使用 addHeaderViewaddFooterView ListView 的方法。

一般建议:
更好地构建标记。确定最高抽象级别的主要元素,然后填充它们。

在你的情况下,我会做这样的事情(如果我正确理解你想要实现的目标):

这只是布局和 ViewGroup 的骨架,你必须用更合适的容器替换。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ViewGroup
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <!-- logoTop here -->
    </ViewGroup>

    <ViewGroup
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <!-- ListView and maybe some overlaying views here -->
    </ViewGroup>

    <ViewGroup
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <!-- barDown here -->
    </ViewGroup>
</LinearLayout>

Your layout is very confusing...
At a minimum, I can distinguish the following observations:

  • Used conflicting tags.
    For example in "btnMap" element:

    android:layout_alignParentBottom="true"
    android:layout_centerInParent="true"
    
  • The elements of the internal layer are aligned with respect to elements of senior layer.
    For example "abtHome", wich located inside ScrollView is aligned with respect to "logoTop", wich located at upper level (at the same as ScrollView).
  • ListView is used inside ScrollView. If you want to add the top or bottom of ListView views, which differ from ListView items, but that must to scroll with items of ListView, you must use addHeaderView and addFooterView methods of ListView.

General advice:
Better structuring a markup. Identify the main elements of the highest level of abstraction, and then fill them.

In your case I would do something like this (if I correctly understand what you wanted to achieve):

This is just skeleton of layout and ViewGroup you must replace with a more appropriate containers.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ViewGroup
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <!-- logoTop here -->
    </ViewGroup>

    <ViewGroup
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <!-- ListView and maybe some overlaying views here -->
    </ViewGroup>

    <ViewGroup
        android:layout_width="match_parent"
        android:layout_height="80dp">

        <!-- barDown here -->
    </ViewGroup>
</LinearLayout>
才能让你更想念 2024-12-18 21:29:08

我解决了这个问题。将列表视图放入滚动视图中并不好。所以我所做的是将 1 个文本视图和图像视图放入一个 xml 中,然后在列表适配器中对其进行扩充。

我将这 2 个字段放在另一个 xml 中,比如 listbox2.xml

 <ImageView android:id="@+id/abtHome" android:layout_below="@+id/logoTop"
    android:src="@drawable/abouthome" android:scaleType="fitXY"
    android:layout_width="match_parent" android:maxWidth="230dip"
    android:layout_height="230dip">
</ImageView> 

<TextView android:id="@+id/tourcity" android:layout_below = "@+id/abtHome"
    android:layout_height="wrap_content" android:layout_width="match_parent"
    android:textSize="25dip" android:layout_centerHorizontal="true"
    android:editable="false" android:paddingTop="5dip" android:textColor="@color/white"
    android:paddingBottom="5dip" android:background="#80000000" android:gravity="center"></TextView>

现在在我的列表适配器中,正在这样做,

   public View getView(int position, View convertView, ViewGroup parent) {

    if(position == 0) {
        convertView = mInflater.inflate(R.layout.listbox2, null);
        TextView cityLabel = (TextView) convertView.findViewById(R.id.tourcity);
        cityLabel.setText("Some Text");
        return convertView;
    }

    else if (convertView == null || convertView.getTag() == null) {
        convertView = mInflater.inflate(R.layout.listbox, null);
        convertView.setOnClickListener(clickListener);
        holder = new ViewHolder();
        holder.view1 = (TextView) convertView.findViewById(R.id.listText);
        holder.desc = (TextView) convertView.findViewById(R.id.editText1);
        holder.index = (TextView) convertView.findViewById(R.id.index);
        holder.desc.setVisibility(View.GONE);
        holder.priceBtn = (Button) convertView.findViewById(R.id.prcBtn);
        holder.icon = (ImageView) convertView.findViewById(R.id.listIcon);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

        return convertView;
 }

如果位置为 0,我将在保留上述 2 个字段的位置膨胀这个新 xml,否则它会以正常方式继续。

希望我的回答很清楚

I solved the issue. It is not good to fit the list view inside scroll view. So what I did is, placed 1 text view and image view in one xml and inflate that in List Adapter.

I placed these 2 fields in another xml say listbox2.xml

 <ImageView android:id="@+id/abtHome" android:layout_below="@+id/logoTop"
    android:src="@drawable/abouthome" android:scaleType="fitXY"
    android:layout_width="match_parent" android:maxWidth="230dip"
    android:layout_height="230dip">
</ImageView> 

<TextView android:id="@+id/tourcity" android:layout_below = "@+id/abtHome"
    android:layout_height="wrap_content" android:layout_width="match_parent"
    android:textSize="25dip" android:layout_centerHorizontal="true"
    android:editable="false" android:paddingTop="5dip" android:textColor="@color/white"
    android:paddingBottom="5dip" android:background="#80000000" android:gravity="center"></TextView>

Now inside my list adapter, am doing like this

   public View getView(int position, View convertView, ViewGroup parent) {

    if(position == 0) {
        convertView = mInflater.inflate(R.layout.listbox2, null);
        TextView cityLabel = (TextView) convertView.findViewById(R.id.tourcity);
        cityLabel.setText("Some Text");
        return convertView;
    }

    else if (convertView == null || convertView.getTag() == null) {
        convertView = mInflater.inflate(R.layout.listbox, null);
        convertView.setOnClickListener(clickListener);
        holder = new ViewHolder();
        holder.view1 = (TextView) convertView.findViewById(R.id.listText);
        holder.desc = (TextView) convertView.findViewById(R.id.editText1);
        holder.index = (TextView) convertView.findViewById(R.id.index);
        holder.desc.setVisibility(View.GONE);
        holder.priceBtn = (Button) convertView.findViewById(R.id.prcBtn);
        holder.icon = (ImageView) convertView.findViewById(R.id.listIcon);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

        return convertView;
 }

If the position is 0, I am inflating this new xml where I kept the above 2 fields or else it continues in a normal way.

Hope my answer is clear

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文