Вопрос

I am trying to create a layout with image situated 20% size of screen from left, 20% size of screen from right and 30% size of screen from top. I can situate an image as 20% of the distance from the sides but don't know how to combine the percentages from the top and sides simultaneously. Now I'm using android:layout_marginTop to jump from the top edge. I want to use something what will make 30% free space of screen from top on any screen resolution. Please take a look on Fig.1. Thanks a lot for any help.

Fig. 1

XML Code:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="100" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_marginTop="100dp"
        android:layout_weight="60"
        android:src="@drawable/logo_line" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20" />
</LinearLayout>
Это было полезно?

Решение

use a RelativeLayout or wrap the ImageView in a LinearLayout :

---- EDIT ----

on the ImageView : take out the layout_gravity and margin_top attributes and set layout_height to 0dp

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="100" >

<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="20" />

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="60"
    android:orientation="vertical"
    android:weightSum="100" >

<TextView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="30" />
<ImageView
    android:id="@+id/imageView4"
    android:layout_width="match_parent"
    android:layout_height="0dp"
     android:layout_weight="40"
    android:src="@drawable/logo_line" />
</LinearLayout>
<TextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="20" />
</LinearLayout>

Другие советы

I have a dynamic solution for that:

final Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);

final WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = (int) (size.x * 0.6);
params.height = (int) (size.y * 0.6);

That would be to leave 20% off for the height (100% - (20% + 20%)) and same for the width.

---- EDIT ----

The above solution would be to format the layout window itself. To do so with just a layout item, I'd go by this:

  final TextView tv = findViewById(R.id.my_text_view);

  LinearLayout.LayoutParams tvlp = (LinearLayout.LayoutParams) tv.getLayoutParams();

  tvlp.rightMargin *= 0.6;
  tvlp.leftMargin *= 0.6;
  tvlp.topMargin *= 0.6;
  tvlp.bottomMargin *= 0.6;

That would redimension a TextView's layout.

Put the ImageView within another LinearLayout so you can set the horizontal weight(the LinearLayout the ImageView is in) and another for the vertical weight

Edited

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="30" />
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:weigth="40"
        android:weightSum="100" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="60"
        android:src="@drawable/logo_line" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="20" />
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="30" />
    </LinearLayout>

This should work.. important are the right configurations of the Views and the orientation of the LinearLayouts. Create a LinearLayout that has 3 'items' in it with a width matched to their parent. You can set the weight for them all to that creates the vertical(top/bottom) margins. Then the second item in the LinearLayout should be another LinearLayout that creates the horizontal(left/right) margins. This LinearLayout should be oriented horizontal.

This isn't a clean solution for margins. If it doesn't work you should do it dynamically

Let me know if it works

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top