It's a question about Android layouts. Here's what I eagerly want to get:
Dark-gray is a LinearLayout. Orange is layout X and then Green is a FrameLayout. I need to put Green outside its parent - layout X. The layout hierarchy described can't be changed. The only option is layout X - it could be whatever you want.
Any ideas?
Use -
android:clipChildren="false"
android:clipToPadding="false"
in every parent of your child view, then you put 'left margin' of your view to be negative,which will put your child view outside of parent view and will not even clip the view.
You can't put the Green part in Layout X, because it can not be drawn outside its parents.
So, you should implements a RelativeLayout or FrameLayout (root Layout) as the parents of all of these Views.
And Put the Green View as the childView of the root Layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layout_dark_grey"
android:layout_width="100dp"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/layout_orange"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="#id/layout_dark_grey"
android:orientation="vertical" >
</LinearLayout>
<RelativeLayout
android:id="#+id/layout_green"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="300dp" >
</RelativeLayout>
</RelativeLayout>
Just add android:clipChildren="false" to the parent layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:clipChildren="false">
<LinearLayout
android:layout_marginLeft="-25dp"
android:background="#color/colorGray"
android:layout_width="100dp"
android:layout_height="100dp"/>
<LinearLayout
android:gravity="center|left"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#color/colorOrange">
<LinearLayout
android:layout_marginLeft="-25dp"
android:background="#color/colorGreen"
android:layout_width="50dp"
android:layout_height="50dp"></LinearLayout>
</LinearLayout>
</LinearLayout>
Related
I mean how can i locate a ImageView in two layouts? i have 2 relative layouts one is up, one is down. The up one has a ImageView but i want half of this ImageView located in down layout. How can i do that?
EDIT::
Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/activity_horizontal_margin"
android:background="#d2aff4">
<Space
android:id="#+id/center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerInParent="true" />
<RelativeLayout
android:id="#+id/bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/center"
android:background="#87c96d" />
<RelativeLayout
android:id="#+id/top"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:src="#mipmap/ic_launcher" />
</RelativeLayout>
</RelativeLayout>
In this scenario the "top" layout needs to be created at last to be displayed on top of the rest of the views. If you need it to have a background colour, you need to apply the colour to the main container.
Here is the result:
I agree with #Booger answer's but If your parent layout is RelativeLayout then add this below property in you ImageView.
android:layout_centerInParent="true"
Or you can use below code to create that kind of layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/gray_font">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/colorAccent">
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher"
android:layout_centerInParent="true"/>
</RelativeLayout>
Take a look at this library https://github.com/umano/AndroidSlidingUpPanel
if you want something easy you can put those layouts in to a parent relative layout and then you can add a image view in parent relative layout as last child and position it how you need
I think you should wrap both your RelativeLayout in another Parent layout, and in that layout, you will place your ImageView (which will span both the other layouts.
Something like this pseudo-code:
<LinearLayout>
<RelativeLayout1>
<RelativeLayout2>
<ImageView gravity=center> //Order counts, this needs to be after the RelativeLayouts to show on top of them
</LinearLayout>
I am trying to achieve this layout
<RelativeLayout>
<Button> <!-- Always attached to the top of the parent -->
<RecyclerView>
<Button> <!-- Always attached to the bottom of the parent -->
<RelativeLayout>
Here is the code
<?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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewTemperatureLog"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</android.support.v7.widget.RecyclerView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
My problem is that although the views are placed correctly, whenever I scroll to the last item in the RecyclerView, it is partially hidden behind the bottom Button. How can I place RecyclerView exactly between the 2 buttons no matter how many items are there in the RecyclerView?
You need to tell the RecyclerView to be below and above your buttons.
<?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">
<Button
android:id="#+id/topButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewTemperatureLog"
android:layout_above="#+id/bottomButton"
android:layout_below="#+id/topButton"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<Button
android:id="#+id/bottomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
You must use LinearLayout as a parent when you are specifying layout_weight attributes :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerViewTemperatureLog"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</LinearLayout>
You can always specify the position of the recycler view inside the relative layout. Where you can put it on top, bottom or both of a view.
First of all you need to add ids to the buttons and then you can specify the position of the recycler view
android:layout_below="#+id/button_top" // place the view below the specified view id
android:layout_above="#+id/button_bottom" // place the view above the specified view id
Click here see the full list on how to position the view inside the RelativeLayout
I have a FrameLayout with many children. One of the children is a LinearLayout. I want the width of the LinearLayout to match_parent but about 90% so; which means I want to add some sort of paddingLeft/Right or margingLeft/Right. But when I add the padding or margin, it is not applied in the Graphical Layout. How do I do this correctly?
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:background="#color/red"
android:marginLeft="20dp"
android:marginRight="20dp"
android:orientation="vertical" >
....
you have
android:marginLeft="20dp"
android:marginRight="20dp"
instead change it to:
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
If it still doesnot work, put this layout inside another layout and set the margins to the other layout.
You worte following code, which is not correct
android:marginLeft="20dp"
android:marginRight="20dp"
Try This :-
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
**android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"**
android:background="#android:color/white"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
Similarly you can give margin at Top and Bottom
I try to decorate ListView items which have overlayed orange bar at the leftside of the item as attached image below.
The layout XML file is as below.
<?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="wrap_content"
android:descendantFocusability="blocksDescendants"
>
<LinearLayout
android:id="#+id/linearLayoutQuestionItemBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="3dp"
android:paddingBottom="4dp"
android:paddingRight="3dp"
android:paddingLeft="10dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:background="#android:color/white"
>
<!-- SOME VARIABLE HEIGHT CONTENTS -->
</LinearLayout>
<!-- leftside orange bar decoration -->
<LinearLayout
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#color/orange"
android:orientation="vertical"
android:layout_marginLeft="12dp"/>
</RelativeLayout>
The XML code above has LinearLayout as a decorate child, but it doesn't matter to use RelativeLayout or other methods.
StackOverflow articles have activity-based approach but I need to use in ListView items with Adapter. what can I do?
== EDIT ==
Sadly, I cannot accept both 2 answers below, because two answers are identical to use LinearLayout (stacked left to right) as parent. Maybe there is no way to fill child height to parent as overlayed with RelativeLayout....
// try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#android:color/white"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#android:color/holo_orange_dark"
android:orientation="vertical"/>
<LinearLayout
android:id="#+id/linearLayoutQuestionItemBody"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
Have you try this..
change the main relativelayout as LinearLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
>
and LinearLayout height match_parent
<LinearLayout
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#color/orange"
android:orientation="vertical"
android:layout_marginLeft="12dp"/>
Can't figure out how to set this up in XML...
Trying to get a button to lay over 2 fragments that sit behind it.
What I want:
What I have:
My code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="horizontal"
tools:context=".VulgarActivity"
>
<Button
android:id="#+id/luckBtn"
android:layout_gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/feel_lucky"
android:textColor="#ffffff"
/>
<fragment
android:name="com.wizardknight.visionaryvulgarity.FirstWord"
android:id="#+id/firstWord"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
<fragment
android:name="com.wizardknight.visionaryvulgarity.LastWord"
android:id="#+id/lastWord"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
/>
</LinearLayout>
You can do this with a RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/firstWord"
android:name="com.wizardknight.visionaryvulgarity.FirstWord"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/lastWord"
android:name="com.wizardknight.visionaryvulgarity.LastWord"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<Button
android:id="#+id/luckBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom"
android:text="#string/feel_lucky"
android:textColor="#ffffff" />
</RelativeLayout>
Explanation:
The difference between RelativeLayout and LinearLayout is simple. LinearLayouts order their child views, like the name already says, linearly. That means, that the child will order themselves, depending on the orientation of the LinearLayout, vertically or horizontally to each other. The child view of a RelativeLayout on the other hand order themselves relative to the parent view or to other views. In this layout I just changed the parent layout to RelativeLayout and wrapped your fragments in a LinearLayout, so it they can use the layout_weight attribute. Then I set this to the button:
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
This aligns the button to the bottom and centers them horizontally. Now, since it does this relative to the top level layout, it overlaps the fragment.