Android的布局调整父无调整孩子的 [英] android layout resizing parent without resizing the childs

查看:145
本文介绍了Android的布局调整父无调整孩子的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TLDR版本:

当我调整父:

说明:

在一个Android应用程序,我有一个父布局,它的多个相对布局内。

in an android application, I have a parent layout and inside it multiple relative layouts.

<LinearLayout
    android:id="@+id/parent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <RelativeLayout
        android:id="@+id/child1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/transparent">

     </RelativeLayout>

    <RelativeLayout
        android:id="@+id/child2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/transparent">

     </RelativeLayout>
</LinearLayout>

此布局的孩子们在它的一些文字和图片。我在做什么是以下内容:

The children of this layout have some texts and pictures in it. What I'm doing is the following:

当用户点击一个按钮,我要崩溃父线性布局(动画宽度,直至为0)。我已经实现这个功能做到这一点:

When the user clicks on a button, I want to collapse the parent linear layout (animate width until it is 0). I have implemented this function to do it:

public static void collapseHorizontally(final View v) {
    final int initialWidth = v.getMeasuredWidth();

    Animation a = new Animation()
    {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            if(interpolatedTime == 1){
                v.setVisibility(View.GONE);
            }else{
                v.getLayoutParams().width = initialWidth - (int)(initialWidth * interpolatedTime);
                v.requestLayout();
            }
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    // 1dp/ms
    long speed = (int)(initialWidth / v.getContext().getResources().getDisplayMetrics().density);
    a.setDuration(speed);
    v.startAnimation(a);
}

我呼吁父视图上述功能。
这是正常工作,并且父视图的宽度调整,直到它到达0,然后我把他的知名度是没有了。

I am calling the above function on the parent view. This is working correctly, and the parent view is resizing in width until it reaches 0, then I set his visibility as gone.

我的问题

虽然父视图重新调整大小(折叠)子视图也与它调整。我的意思是,当父的宽度达到子之一,子元素也将与它重新大小,这将导致textviews在它太重新大小

While the parent view is re-sizing (collapsing) the child views are also resizing with it. what I mean is that when the width of the parent reaches one of the child, the child element will also re-size with it and it will cause the textviews in it to re-size too.

下面是一个例子一些截图:

Here is some screenshots for an example:

在这里,您可以看到初始布局,有绿色父布局和子布局(日期和文字和图标)。

Here you can see the initial layout, there is the parent layout in green, and the child layout (with the date and text and icon).

然后,当我开始减少母体布局的大小,子布局大小也将受到影响,这将降低它的TextView中的大小,使字来包装,如以下2个图像看出:

Then when I start to reduce the size of the parent layout, the child layout size will also be affected and it will reduce the size of the textview in it, causing the words to wrap as seen in the following 2 images:

当你发现为父母的宽度减少在孩子中的文本被包裹。

As you notice the text in the child is being wrapped as the width of the parent is reduced.

有没有一种方法我可以有子元素不与父视图调整大小,但仍保持原样,即使父视图大小减少?我需要的元素柒全伸缩动画NAD期间保持固定的大小得到由母公司削减,而不是用它调整。

Is there a way I can have the child element to not resize with the parent view, but remain as it is, even if the parent view size is reducing? I need the size of the chil element to stay fixed during the whole resizing animation nad get cut by the parent instead of resizing with it.

非常感谢您的任何帮助。

Thank you very much for any help

推荐答案

您的问题来自几个问题。

Your problem comes from several issues.


  • 第一:在 SINGLELINE =的假参数的 TextViews >
    使用此参数,你不能要求它来包装其内容,因为文本可以使用多条线路,因此没有一个适当的宽度。如果有一个以上的线将填补其母公司。

  • 第二:WRAP_CONTENT的行为以适应内容大小,除非它比母大。在这种情况下,将再次匹配父大小。 (我们可以看到,从填充或保证金的动作,比边框下面父大小。)

  • first : the singleLine="false" parameter on your TextViews > With this parameter, you can't ask it to wrap its content, since the text can use multiple lines and thus don't have a proper width. If there are more than one line it will fill its parent.
  • second: The behaviour of "wrap_content" is to fit the content size unless its bigger than the parent. in this case, it will match the parent size again. (we can see, from padding or margin movements, than borders are following the parent size.)

第二个问题可以通过使用固定的大小,但其不与所述第一点的情况下得到解决:

The second issue can be solved by using fixed size but its not the case with the first point :

您可以设置 SINGLELINE =真正的的android:宽=WRAP_CONTENT来看看它的工作在一行。 (集的android:ellipsize =无,以隐藏'...'走动)
但单行文本是不是你所需要的吧?

You could set singleLine="true", with android:width="wrap_content" to see it working on a single line. (set android:ellipsize="none" in order to hide the '...' moving around) But a single line text isn't what you need right ?

我已经为了避免这些视图调整大小,即使多行延长的TextView 作了测试:

I've made tests with a extend of TextView in order to avoid those views from resizing, even with multiple lines :

public class NoResizeTextView extends TextView {

    int firstWidth = -1;
    int firstHeight = -1;

    public NoResizeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        Layout layout = getLayout();
        if (layout != null) {

            if (firstWidth == -1)
                firstWidth = getMeasuredWidth();
            if (firstHeight == -1)
                firstHeight = getMeasuredHeight();

            setMeasuredDimension(firstWidth, firstHeight);
        }
    }

}

然后用这个TextView的延长您的布局:

Then use this TextView extend in your layout :

       <com.guian.collapsetest.NoResizeTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="none"
                android:singleLine="false"
                android:text="Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit" >
        </com.guian.collapsetest.NoResizeTextView>

这不是真的很漂亮,我不建议使用太多,因为它可以打破Android的布局mecanism。但是你的具体情况,我想它的工作。

That's not really beautiful and I don't advise to use it too much since it could break android's layout mecanism. But in your particular case, I guess it does the job.

逻辑:

在扩展视图中创建一个自定义的,你有责任 onMeasure 的实施。这是你的看法是什么给它的大小。所以这是它如何工作的:你计算一次的大小(或让默认功能为你做感谢名单以 getMeasuredW / H ),并保存它,然后在尺寸为每个请求,你只需返回相同的值,以便您的视图大小不会改变。

When you extend a view to create a custom one, you are responsible for the implementation of onMeasure. This is what give its size to your view. So that's how it works : you compute once the size ( or let the default function do it for you thanx to getMeasuredW/H) and save it, then on each request for size, you just return the same values so your view size won't change.

限制:

正弦波您的视图大小不会改变,改变屏幕的方向/家长大小或再次何时能有一个坏的行为,里面的时候更改文本(如维克拉姆说的)。
这就是我所谓的打破Android的布局mecanism。
如果您需要动态地更改文本,你将不得不延长setText方法,使其能够在这一点上调整...

Sine your view size won't change, it can have a bad behaviour when changing screen orientation / parent size or again, when the text inside change (As Vikram said). That's what I called "break android layout mecanism". If you need to change the text dynamically, you would have to extend the setText method to allow it to resize at this point ...

这篇关于Android的布局调整父无调整孩子的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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