Coder Social home page Coder Social logo

mmin18 / flexlayout Goto Github PK

View Code? Open in Web Editor NEW
1.4K 59.0 115.0 511 KB

A powerful Android layout view that use java expression in layout params to describe relative positions.

License: Other

Java 100.00%
android flex-layout relativelayout linearlayout

flexlayout's Introduction

FlexLayout

The idea is simple, use java expressions in layout params like layout_left="view1.right+10dp". It is helpful when LinearLayout and RelativeLayout is not enough for you.

IMG

<TextView
	app:layout_left="icon.right+10dp"
	app:layout_right="100%-14dp"
	app:layout_centerY="icon.centerY"
	android:layout_height="wrap_content"
	.../>

Try the sample apk: FlexLayout.apk

Adding to project

Add dependencies in your build.gradle:

	dependencies {
	    compile 'com.github.mmin18:flexlayout:1.2.7'
	}

Or if you are using Eclipse, just copy FlexLayout.java and attrs.xml to your project.

Layout Params

Horizontal Vertical
layout_left layout_top
layout_right layout_bottom
layout_centerX layout_centerY
layout_width layout_height

Remember the app:layout_width is different from android:layout_width
xmlns:app="http://schemas.android.com/apk/res-auto"

% Percentage

IMG

<Button
	app:layout_left="10%"
	app:layout_right="90%"
	app:layout_centerY="50%"
	android:layout_height="wrap_content"
	../>

or

<Button
	app:layout_width="80%"
	app:layout_centerX="50%"
	app:layout_centerY="50%"
	android:layout_height="wrap_content"
	../>

Reference other views

Reference previous view using prev, next view using next (Position in the XML layout file)

IMG

<View ../>        // prev = Previous view in xml layout file

<View
	app:layout_left="prev.right"
	app:layout_right="next.left"
	app:layout_top="prev.top"
	app:layout_bottom="next.bottom" />

<View ../>        // next = Next view in xml layout file

Reference a specific view using view's id

IMG

<View
	app:layout_left="view1.right"
	app:layout_right="android:text1.left"
	app:layout_top="view1.top"
	app:layout_bottom="android:text1.bottom" />

<View android:id="@+id/view1"
	../>
<View android:id="@android:id/text1"
	../>

You can also use parent to reference the FlexLayout and this to reference the child view itself. Use screen to reference screen size.

Keyword Target
prev Previous view in XML layout
next Next view in XML layout
view_id <View id="@+id/view_id" /> defined in the same layout
this The view itself
parent The parent FlexLayout, doesn't support left top right bottom centerX centerY
screen Screen size (getResources().getDisplayMetrics(), only support width and height)
Properties Value
left top
right bottom
centerX centerY
width height
visible view.getVisibility() == View.VISIBLE
gone view.getVisibility() == View.GONE
tag view.getTag(), only support Number or Boolean. Other types or null returns 0

(When use with view.tag, after View.setTag() you should call View.requestLayout() to trigger layout.)

Expression

The syntax is the same as Java or C. Numbers can have units like 10dp, 15sp

(parent.height-view1.centerY)/2
100%-80dp
max(view1.right, view2.right)
screen.width<screen.height ? 64dp : 48dp
view1.visible && view2.visible ? max(view1.bottom, view2.bottom) : 0px

Operators (Order in precedence)

Operator Associativity
() sp dp dip px pt mm in Right
! Right
* / % Left
+ - Left
<= < >= > Left
== != Left
&& Left
ll Left
?= Right

Functions

Name
max(a,b)
min(a,b)
round(a)
ceil(a)
floor(a)
abs(a)
mod(a)
pow(a)

dimens.xml

Of course you can reference dimensions defined in res/values/dimens.xml

<View
	app:left="@dimen/default_margin"
	app:bottom="50%-@dimen/default_margin"
	app:width="2*@android:dimen/app_icon_size"
	../>

wrap_content

You can use wrap_content and match_parent as a normal value in expression, like app:layout_width="min(wrap_content, 80dp)" which is equievalent to android:maxWidth="80dp".

Using wrap_content in expression is more flexable than using android:maxWidth / android:minWidth. For example, you want to put an icon to the right of a TextView:

IMG

<TextView
	app:layout_width="min(wrap_content, 100%-next.width)"
	android:layout_height="wrap_content"
	android:text="Either short or long text"
	android:singleLine="true"
	... />
<ImageView
	android:layout_width="wrap_content"
	android:layout_height="wrap_content"
	app:layout_left="prev.right"
	android:src="@drawable/info"
	... />

Benchmark

The following benchmark is done by Piasy, and you can check the details here.

Simple Layout

inflate (ns) measure (ns) layout (ns)
RelativeLayout 3325842 947464 108585
FrameLayout 3159841 879161 112988
FlexLayout 5278923 796837 111414

Complex Layout

inflate (ns) measure (ns) layout (ns)
RelativeLayout 17479435 2268045 822163
GridLayout 20350271 3270156 1177185
FlexLayout 21698676 2703914 1001549

You can check the layout xml files here

FlexLayout usually takes longer to inflate, but it's equally fast in measure and layout. Normally you use less hierarchy and views than RelativeLayout or LinearLayout, so the overall time spend is competitively, especially when comes to complex layouts.

Changelog

1.2.7 (2017-8-2)

Avoid crash when layout is empty.

1.2.6 (2016-9-28)

Support Arabic RTL (layoutDirection). Simply flip everything from right to left.

1.2.5 (2016-9-25)

Allow restriction conflict like both left, right and width is defined (width will be ignored)

1.2.4 (2016-6-02)

Fix #8, TextView clipped issue with wrap_content expression.

1.2.3 (2016-4-25)

Use wrap_content and match_parent as a normal value in expression.

1.2.2 (2016-4-17)

Support AndroidStudio Preview (Fix view reference in IDE preview)

1.2.1 (2016-4-8)

Support parent.visible, parent.gone, parent.tag

1.2.0 (2016-4-6)

Show source code position in XML when throw Exceptions. (Syntax exception, Circular dependency, etc.)

1.1.0 (2016-3-20)

Initial release to jcenter. Including percentage, view reference, ?= expressions, logic operators.

flexlayout's People

Contributors

mmin18 avatar piasy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flexlayout's Issues

`app:layout_width="min(wrap_content, 100% - 55dp)"` text clipped

Layout code:

<com.github.mmin18.widget.FlexLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

    <TextView
            app:layout_width="min(wrap_content, 100% - 55dp)"
            android:layout_height="wrap_content"
            android:text="Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! Hello World! End"
            />
    <ImageView
            android:layout_width="55dp"
            android:layout_height="55dp"
            app:layout_right="100%"
            />
</com.github.mmin18.widget.FlexLayout>

text got clipped (End not visible):

screen shot 2016-06-01 at 12 23 54

Docs: Clarify prev/next Meaning

Reference previous view using prev, next view using next

"Previous" and "next" in terms of... what?

  • Position in the XML layout file?
  • Location with respect to the property? (e.g., for app:layout_left="prev.right", prev means "whatever's closest to us on the left")
  • Something else?

The rest of the rules seem straight-forward, but IMHO this description could use a bit more love.

Thanks!

Setting views relative to another view

Hi

When I am trying to set views relatively to each other I a getting following error. I have taken care of defining references view first.

I am using Android studio 2.0. I am facing this issue in my demo app as well as sample app along with this library.

(Reference a specific view using view's id)

java.lang.IllegalArgumentException: unknown identifier view2, layout_top=view2.bottom+20dp
at com.github.mmin18.widget.FlexLayout$TokenReader.parseStr(FlexLayout.java:1334)
at com.github.mmin18.widget.FlexLayout$TokenReader.readToken(FlexLayout.java:1286)
at com.github.mmin18.widget.FlexLayout$RPN.parse(FlexLayout.java:1420)
at com.github.mmin18.widget.FlexLayout$LayoutParams.(FlexLayout.java:78)
at com.github.mmin18.widget.FlexLayout.generateLayoutParams(FlexLayout.java:325)
at com.github.mmin18.widget.FlexLayout.generateLayoutParams(FlexLayout.java:32)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:837)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:811)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:229)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:426)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:510)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:498)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:967)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:498)
at com.android.tools.idea.rendering.RenderTask.access$600(RenderTask.java:72)
at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:610)
at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:607)
at com.android.tools.idea.rendering.RenderService.runRenderAction(RenderService.java:359)
at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:607)
at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:629)
at com.intellij.android.designer.designSurface.AndroidDesignerEditorPanel$7.run(AndroidDesignerEditorPanel.java:519)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:337)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:327)
at com.intellij.util.ui.update.MergingUpdateQueue$3.run(MergingUpdateQueue.java:271)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:286)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:244)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:234)
at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
at com.intellij.util.Alarm$Request$1.run(Alarm.java:352)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
at java.lang.Thread.run(Thread.java:695)

Here is my sample XML file...

<com.github.mmin18.widget.FlexLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
    app:layout_width="(152/3.84)%"
    app:layout_height="(64/5.70)%"
    android:text="New Button"
    android:id="@+id/button"
    app:layout_centerX="50%"

    app:layout_top="(73.68%)"/>

<ImageView

    app:layout_width="(116/3.84)%"
    app:layout_height="(94/5.70)%"
    app:layout_top="(25/5.70)%"
    app:layout_left="0dp"
    android:id="@+id/imageView2"
    android:background="@color/abc_search_url_text_pressed"/>

<TextView
    android:id="@+id/view2"
    app:layout_width="(244/3.84)%"
    app:layout_height="(32/5.70)%"
    android:text="New Text1"
    app:layout_left="(122/3.84)%"
    app:layout_top="(32/5.70)%"/>


<TextView
    android:id="@+id/textView3"
    android:layout_width="244dp"
    android:layout_height="32dp"
    android:text="New Text"
    app:layout_centerX="view2.centerX"
    app:layout_top="view2.bottom+20dp"

    />

</com.github.mmin18.widget.FlexLayout>

Let me know if I am missing anything.

FlexLayout 性能测试

其实这不是一个 issue 😃

首先点12个赞,FlexLayout 真的太棒了!

我对 RelativeLayout, FlexLayout 以及其他 layout 实现同样的 UI 的性能进行了一个测试,测试结果传送门:http://blog.piasy.com/2016/04/07/Layout-Perf/,总的来说,非列表情况下,FlexLayout 的性能几乎毫不逊色,但如此强大优雅的方式,何乐而不用呢!

动态布局并且定位在前后布局中间时不显示的BUG

<TextView
            android:id="@+id/View1"
            app:layout_width="min(wrap_content, 100%-next.width-prev.width)"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="1"
            app:layout_left="prev.right"
            app:layout_right="next.left"
            app:layout_top="prev.top" />

当布局是这种定位的时候该TextView最后不显示(应该是同时定义了layout_left和layout_right的原因),只定义layout_left,在下一个View里也定义layout_left则可以生效

@dimen 相关计算无法使用

使用类似 readme 中的

app:bottom="50%-@dimen/default_margin"
app:width="2*@android:dimen/app_icon_size"

会无法预览, 提示can`t resolve symbol

关于TextViiew设置layout_top和layout_bottom后内容居中问题

TextViiew设置layout_top和layout_bottom后,设置gravity无法居中。
如:
<TextView android:background="@color/color4" android:gravity="center" android:text="这是TextView" app:layout_height="this.width*0.2"a app:layout_centerY="50%" app:layout_left="20%" app:layout_centerX="50%" app:layout_right="90%" app:layout_bottom="prev.top" app:layout_top="view1.bottom"/>
即:同时设置android:layout_height 和 :layout_bottom、layout_top
实际显示高度按app:layout_bottom、app:layout_top显示,但是内部居中是按android:layout_height显示
除非删除android:layout_height 才正常显示

Fail to preview in Android Studio 2.2 preview 3

Android Studio 2.2 has introduced the New UI Designer & Constraint Layout, but in the preview tab of AS, it can't preview my layout, the stack trace is:

java.lang.NoSuchFieldError: FlexLayout_Layout
    at com.github.mmin18.widget.FlexLayout$LayoutParams.<init>(FlexLayout.java:104)
    at com.github.mmin18.widget.FlexLayout.generateLayoutParams(FlexLayout.java:353)
    at com.github.mmin18.widget.FlexLayout.generateLayoutParams(FlexLayout.java:36)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:860)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:834)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:317)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429)
    at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:389)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:548)
    at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:533)
    at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:962)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:533)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$58(RenderTask.java:659)
    at com.android.tools.idea.rendering.RenderService.runRenderAction(RenderService.java:380)
    at com.android.tools.idea.rendering.RenderTask.inflate(RenderTask.java:659)
    at com.android.tools.idea.uibuilder.model.NlModel.inflate(NlModel.java:327)
    at com.android.tools.idea.uibuilder.model.NlModel.render(NlModel.java:384)
    at com.android.tools.idea.uibuilder.editor.NlPreviewForm$Pending.<init>(NlPreviewForm.java:234)
    at com.android.tools.idea.uibuilder.editor.NlPreviewForm.setFile(NlPreviewForm.java:293)
    at com.android.tools.idea.uibuilder.editor.NlPreviewManager$3.run(NlPreviewManager.java:290)
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:333)
    at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:323)
    at com.intellij.util.ui.update.MergingUpdateQueue$3.run(MergingUpdateQueue.java:267)
    at com.intellij.util.ui.UIUtil.invokeLaterIfNeeded(UIUtil.java:2440)
    at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:279)
    at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:234)
    at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
    at com.intellij.util.Alarm$Request$1.run(Alarm.java:378)
    at com.intellij.util.Alarm$Request.run(Alarm.java:398)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at com.intellij.util.concurrency.SchedulingWrapper$MyScheduledFutureTask.run(SchedulingWrapper.java:227)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:857)
    at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:658)
    at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:386)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

TextView宽度测量不对的问题

<TextView
        android:text="Long text long text long text long text long text long text long text long text long text long text long text END"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/color2"
        app:layout_top="30dp"
        app:layout_left="30dp"
        app:layout_right="parent.width"
        />

9k 1 zjs qwte jmue 9 g

当布局已指定该TextView的left 、right时,宽度仍然以parent的宽度测量导致显示异常。已提交pull requests修复该问题。

Anko feature

Please can you add Anno (Kotlin) features for you lib?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.