与LinearLayout中对滚动型中对儿童体重的属性 [英] LinearLayout with weight-attributes on children inside a ScrollView

查看:98
本文介绍了与LinearLayout中对滚动型中对儿童体重的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关ScrollViews通常的问题似乎是如何使用fillViewport让滚动型延伸到整个屏幕。我的问题是相似,但走另一条路:)

我得到了一个包含多个元素的LinearLayout中。所有除一个元素有一个固定的高度,特别的是应该延伸到填充剩余空间ImageView的。这工作得很好:)

现在我想把的LinearLayout成滚动型,因为我的一些元素将会在运行时扩大(如点击更多-icon用于扩展一个TextView)。在未展开的版本,我想有配合屏幕上的所有元素,如果滚动型就不存在了。

不幸的是,包裹周围的LinearLayout的滚动型时,ImageView的缩放到最大范围UND画面不适合屏幕。你有什么想法如何实现我的目标是什么?

我转载问题的示例应用程序与你分享:

 <滚动型的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:fillViewport =真正的>    <的LinearLayout
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直>        < ImageView的
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =0像素
            机器人:layout_weight =1
            机器人:背景=#FFFF0000
            机器人:scaleType =fitCenter
            机器人:SRC =@绘制/背景/>    <的TextView
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =80dp
        机器人:背景=#FF00FF00
        机器人:文本=元素2
        工具:上下文=。MainActivity/>        <的TextView
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =80dp
            机器人:背景=#FF00FF00
            机器人:文本=1元
            工具:上下文=。MainActivity/>
    < / LinearLayout中>< /滚动型>

下面是从两个版本的截图:

上面的一个是滚动型与布局(注意滚动条和种植单元1),下面的一个没有。

更新:在图像查看图像的原始高度比屏幕大。因此,应缩小(这就是为什么它有重量1和scaleType集)。

解决方案:下面code解决了这个问题对我来说(基于Luksprog答案)

主要活动(节选,布局的变化是由点击ImageView的诱导):

  @覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    滚动视图=(滚动型)findViewById(R.id.scroller);
    帧=(的FrameLayout)findViewById(R.id.frame);
    布局=(的LinearLayout)findViewById(R.id.layout);    最终ImageView的图像=(ImageView的)findViewById(R.id.image);
    image.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            ImageView的图像=(ImageView的)findViewById(R.id.image);
            frame.removeView(布局);
            scrollView.addView(布局);
            image.getLayoutParams()高度= image.getHeight()。
        }
    });}

布局XML文件

 <的FrameLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID /帧
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
     >    <滚动型
        机器人:ID =@ + ID /滚轮
        机器人:layout_height =FILL_PARENT
        机器人:layout_width =FILL_PARENT
        机器人:fillViewport =真
        >    < /滚动型>    <的LinearLayout
        机器人:ID =@ + ID /布局
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直>        < ImageView的
            机器人:ID =@ + ID /图像
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =0像素
            机器人:layout_weight =1
            机器人:背景=#FFFF0000
            机器人:scaleType =fitCenter
            机器人:SRC =@绘制/背景/>        <的TextView
            机器人:ID =@ + ID / text1中
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =80dp
            机器人:背景=#FF00FF00
            机器人:文本=元素2
            工具:上下文=。MainActivity/>        <的TextView
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =80dp
            机器人:背景=#FF00FF00
            机器人:文本=1元
            工具:上下文=。MainActivity/>
        <的TextView
            机器人:layout_width =FILL_PARENT
            机器人:layout_weight =0.1
            机器人:layout_height =0像素
            机器人:背景=#FF00FF00
            机器人:文本=元素3
            工具:上下文=。MainActivity/>
    < / LinearLayout中>< /&的FrameLayout GT;


解决方案

正如我在评论说,我不知道,如果你试图做的XML水平是可能的。一种选择是修改当前的布局,并添加根的FrameLayout 来,你会,最初,添加滚动型和它上面的的LinearLayout 。在这个位置上的的ImageView 将表现为你想要的用户没有修改布局。当你需要表现出更多的在你的布局你会分离的LinearLayout 从视图层次结构(与 removeView )和它连接到滚动型(用 addView )。当用户返回到初始布局你会扭转这种

这将使的ImageView 使得当切换的意见,所以你会希望获得的ImageView 并做开关时,重新设置。这个高度,你可以做一个听众或使用的onCreate 方法(如意见避风港't在那一刻已经奠定了没有)。此外,请记住用户可以打开手机,我不知道这是否会影响到你的布局,但把它考虑。

The usual question regarding ScrollViews seems to be how to use fillViewport to let the ScrollView stretch to the whole screen. My problem is similar, but going the other way :)

I got a LinearLayout that contains multiple elements. All except one element have a fixed height, the special one is an ImageView that should stretch to fill the remaining space. This works fine :)

Now I want to put the LinearLayout into a ScrollView, as some of my elements should expand at runtime (e.g. click on "more"-icon that expands a TextView). In the unexpanded version I would like to have all elements fit on the screen, as if the ScrollView wouldn't exist.

Unfortunately, when wrapping the ScrollView around the LinearLayout, the ImageView is scaled to maxSize und the screen does not fit the screen. Do you have any ideas how to achieve my goal?

I reproduced the problem in an example app to share it with you:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="fill_parent"
            android:layout_height="0px"
            android:layout_weight="1"
            android:background="#ffff0000"
            android:scaleType="fitCenter"
            android:src="@drawable/background" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:background="#ff00ff00"
        android:text="element 2"
        tools:context=".MainActivity" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:background="#ff00ff00"
            android:text="element 1"
            tools:context=".MainActivity" />
    </LinearLayout>

</ScrollView>

Here are the screenshots from the two versions:

The one above is the Layout with ScrollView (notice the scrollbar and cropping on element 1), the one below without.

Update: The original height of the image in the image-view is bigger than the screen. So it should be downscaled (that's why it has weight 1 and scaleType set).

Solution: The following code solved the problem for me (based on Luksprog answer)

Main Activity (excerpt, change of layout is induced by click on ImageView):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    scrollView = (ScrollView) findViewById(R.id.scroller);
    frame = (FrameLayout) findViewById(R.id.frame);
    layout = (LinearLayout) findViewById(R.id.layout);

    final ImageView image = (ImageView) findViewById(R.id.image);
    image.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            ImageView image = (ImageView) findViewById(R.id.image);
            frame.removeView(layout);
            scrollView.addView(layout);
            image.getLayoutParams().height = image.getHeight();
        }
    });

}

Layout XML file

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <ScrollView 
        android:id="@+id/scroller"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:fillViewport="true"
        >

    </ScrollView>

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/image"
            android:layout_width="fill_parent"
            android:layout_height="0px"
            android:layout_weight="1"
            android:background="#ffff0000"
            android:scaleType="fitCenter"
            android:src="@drawable/background" />

        <TextView
            android:id="@+id/text1"
            android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:background="#ff00ff00"
            android:text="element 2"
            tools:context=".MainActivity" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="80dp"
            android:background="#ff00ff00"
            android:text="element 1"
            tools:context=".MainActivity" />
        <TextView
            android:layout_width="fill_parent"
            android:layout_weight="0.1"
            android:layout_height="0px"
            android:background="#ff00ff00"
            android:text="element 3"
            tools:context=".MainActivity" />
    </LinearLayout>

</FrameLayout>

解决方案

As I said in my comments, I don't know if what you're trying to do is possible at the xml level. One option would be to modify your current layout and add a root FrameLayout to which you'll, initially, add the ScrollView and above it, the LinearLayout. In this position the ImageView will behave as you want as the user didn't modify the layout. When you need to show more in your layout you'll detach the LinearLayout from the view hierarchy(with removeView) and attach it to the ScrollView(with addView). You'll reverse this when the user goes back to the initial layout.

This will make the ImageView to have different heights when making the views switch so you'd want to get the height of the ImageView and re set it when doing the switch. To get the height, you could do it in a listener or using the post method in onCreate(as the views haven't been laid out yet at that moment). Also, remember that the user could turn the phone, I don't know if this will affect your layout, but take it consideration.

这篇关于与LinearLayout中对滚动型中对儿童体重的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆