I'm using a view with only 10dp height, so it just looks like a line.
If my layout_width is greater than a certain number (for example if I set layout_width=900dp), the view doesn't take the width... It seems that there is a max width for a view.
Does anyone know ?
Thanks.
Yan.
EDIT : Sorry, here's my xml layout.
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.test.MainActivity" >
<View
android:background="#color/black"
android:layout_width="900dp"
android:layout_height="10dp"
android:transformPivotX="5dp"
android:rotation="45"/>
</RelativeLayout>
EDIT 2 : Here's the result. Why the view can't go further ?
http://image.noelshack.com/fichiers/2014/40/1411963572-screenshot-2014-09-28-22-36-32.png
It's because it's width and height are limited by the parents bounds, if you didn't rotate it you'll see that it just takes the parents width.
A View object's measured width and measured height values must respect
the constraints imposed by the View object's parents. This guarantees
that at the end of the measure pass, all parents accept all of their
children's measurements.
Source
If what you want is to draw a rotated line, you can consider doing so directly with a custom view. Follow this to get a head start
Related
I'm using this library to create a circular list. I want put items exactly in center of screen for all phones with different screen sizes.
How can i do that in a short way ?
We can use app:wheelRadius="350dp" to change radius of out circle ( wheel ). can i set a dynamic value for this to put elements in center of screen for all phones ?
XML
<RelativeLayout
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">
<com.lukedeighton.wheelview.WheelView
android:id="#+id/wheelview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:repeatItems="true"
app:rotatableWheelDrawable="false"
app:selectionAngle="90.0"
app:wheelItemCount="14"
app:wheelItemRadius="25dp"
app:wheelOffsetY="100dp"
app:wheelPadding="13dp"
app:wheelPosition="bottom"
app:wheelRadius="350dp"/>
</RelativeLayout>
We have to parameter here. wheelOffsetY and wheelRadius that control wheel postition
I want to display an image as background in my app. Now I used this statement: android:background="#drawable/background" in <LineraLayout>. Here is the .xml Code:
<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="vertical"
tools:context="overbit.user.MainActivity"
android:background="#drawable/background">
</LinearLayout>
Now I get this output:
But I want it like this:
Maybe someone of you can help me. Thanks.
There is BitmapDrawable, which allows to do it. First you need to create a drawable resource as follows (let it be a file in the project resources res/drawable/mybg.xml ):
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background"
android:gravity="center" />
And then specify it as a background resource in your layout:
<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="vertical"
tools:context="overbit.user.MainActivity"
android:background="#drawable/mybg">
</LinearLayout>
You need to change the image's size itself in photoshop or something to achieve the desired result (still this can be very difficult because of various screen sizes of android smartphones). A workaround can be to change your parent layout to Relativelayout and then use a an imageview to show your picture like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent" //make sure its match parent
android:layout_height="match_parent"// and this one also
android:src="#drawable/yourBackgroundImage"
android:scaleType="..."/>
<LinearLayout>put your childviews here</LinearLayout>
there are 4-5 options available for android:scaleType... experiment with them, till you get your desired result. Also note that you need to use android:src rather than android:background attribute of the imageview
You can accomplish what you're trying to do by using BitmapRegionDecoder.
From the docs:
BitmapRegionDecoder can be used to decode a rectangle region from an
image. BitmapRegionDecoder is particularly useful when an original
image is large and you only need parts of the image.
It allows you to decode a Rect area of a particular Bitmap fairly easily, the only thing you need to do is calculate your Rect region to decode relative to the Bitmap.
In android the position of an ImageView is determined by the top left side of the image. I am assuming that the x direction is 1/3rd of the width from the start point. Now we can set the pivot. Basically the location of the pivot point, is a location around which the view will rotate and scale.
int displacementX = ImageView.getWidth() / 3;
int displacementY = 10;
imgview.setPivotX((float)displacementX);
imgview.setPivotY((float)displacementY);
Now that we have set the pivot we can use a scale type to fit the image.
imgview.setScaleType(ImageView.ScaleType.CENTER_CROP);
In a linear layout the nested widgets are arranged horizontaly/vertically and according to their width/height.
My question is in the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffff0000"
/>
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff00ff00"
/>
</LinearLayout>
</LinearLayout>
Why do I see only the first view? I was expecting since they were wrap_content either both would display or none. But I see in the design preview:
Also if I add android:layout_weight="2" in the first View and android:layout_weight="1" in the second the second view gets 2/3 of the space even if I add weight_sum=3 in the enclosing layout
You see only one view because you didn't give a numeric width to the views.
Because of that the first view gets all the screen and other one is not seen at all - it is at the right, next to the red one.
if you want that both of them will be with same width, just give each one the same weight, that way they will be equal to each other and will fit to the screen height or width, depends on the layout orientation of the container.
From the documentation:
android:weightSum - Defines the maximum weight sum. If unspecified, the sum is computed by
adding the layout_weight of all of the children.
I researched on another stackoverflow question//answer and found a working definition of layout_weight to be that "This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view"
(source:What does android:layout_weight mean?)
I am trying to apply that concept to my code.
Currently my code (without the layout weights) is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Home"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="About"
android:gravity="center"
/>
</LinearLayout>
And the current layout is http://imgur.com/eq6q0I8
(the home should take up all the space bc of match_parent)
However if i add android:layout_weight="1" to both text views,
i get this layout http://imgur.com/fscM0Zr
(Both are visible and share same amount of space)
My question is how is this happening from that working definition of layout_weight? layout_weight in this example will assign extra space equally to both the text views. But there is no extra space because the first one has width match_parent which will make it take the width of the entire parent(no extra space)
How is this happening ?
To fix the ideas, let's say that LinearLayout's width is 100px.
TextView Home is MATCH_PARENT so 100px
TextView About is MATCH_PARENT so 100px
So, total width used by children is 100px+100px = 200px
Now, let's compute the remaining width : this is the difference between the available width and the width used by all children :
RemainingWidth = AvailableWidth (100px) - TotalWidthUsedByChildren (200px) = -100px
Note that it is negative
There is 2 child views, so distribute the remaining width to each of them according their weight. In this case each child receive 50% of the remaining... it means that each child receive -50px.
So finally :
TextView Home width is 100px + -50px = 50px
TextView About width is 100px + -50px = 50px
Perfectly logic, but not very intuitive. So the recommendation when using layout_weight is to always set the width of chid views to 0.
I want to create a screen
with two fixed size areas: one aligned to left, one to the right
and in between them another area which spans all the rest of the area?
If I make the middle view with property fill_parent it will catch all the area to the 3rd child.
what will be the effective property when layout_weight=0.X and layout_width=20dp.?
======
I have a linearLayout with orientation= horizontal.
It has a child with layout_weight=0.X and also layout_width=20dp.
What will be the effective property?
If you have a horizontal LinearLayout with the first child with a fixed width (e.g. layout_width="20dp"), the second child with a non-zero weight (e.g. layout_weight="1") and the third with a fixed width (e.g. layout_width="20dp"), then you should get the first aligned to the left, the third aligned to the right and the third filling the area between them.
It's also possible to do this with a RelativeLayout, but I'll leave that as the above solution should work just fine.
It will be something like this:
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<View android:id="#+id/view1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<View android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<View android:id="#+id/view3"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_weight="0"/>
</LinearLayout>
You should use android:layout_weight.