Ustaw pozycję / Rozmiar elementu UI jako procent rozmiaru ekranu

Staram się ustalić, czy możliwe jest użycie pozycji procentowych/rozmiarów podczas tworzenia układu. Chcę czegoś takiego...

^
|
|
| 68%
|
|
v
Gallery (height equivalent of 16% total screen size)
^
| 16%
v

Testuję na urządzeniu, które w krajobrazie ma wyświetlacz 800x480 rzeczywistych pikseli i obecnie wymuszam to poniższym...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" > 
    <Gallery 
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="80px"
        android:layout_marginTop ="320px"
    />
</RelativeLayout>

Oczywiście nie chcę kodować stałych jednostek px, ale nie mogę używać 68% lub 0.68 dla layout_marginTop (na przykład). Przeglądałem dp jednostki, ale nie jestem pewien, czy mogę też tak zrób.

Muszę przyznać, że projektowanie UI jest moim słabym punktem, więc każda rada zostanie z wdzięcznością przyjęta.

EDIT: dla odniesienia na przyszłość jeśli ktoś szuka podobnej odpowiedzi, podążając za sugestią Alana Moore ' a, mam następujące działa dokładnie tak, jak chcę...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@drawable/bground"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.68"
        android:background="@android:color/transparent"
    />
    <Gallery 
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.16"
    />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.16" 
        android:background="@android:color/transparent"
    />
</LinearLayout>

Udało mi się znaleźć inne przykłady użycia layout_weight i postanowiłem ustawić TextView wysokości na 0dp, a także użyć pływaków do ciężarów. Działa świetnie. :)

Author: Squonk, 2011-09-21

5 answers

Myślę, że chcesz ustawić Androida: layout_weight,

Http://developer.android.com/resources/tutorials/views/hello-linearlayout.html

Coś takiego (umieszczam widoki tekstu powyżej i poniżej jako placeholdery):

  <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weightSum="1">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="68"/>
    <Gallery 
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="0dp"

        android:layout_weight="16"
    />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="16"/>

  </LinearLayout>
 70
Author: Alan Moore,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-01 13:48:46

Dla TextView i jego potomków (np. przycisku) można uzyskać Rozmiar Wyświetlacza Z Windowmanagera, a następnie ustawić wysokość widoku tekstowego jako jego ułamek:

Button btn = new Button (this);
android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
btn.setHeight((int)(display.getHeight()*0.68));
 18
Author: joseph_morris,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-09-21 20:50:56

Użyj Procentrelativelayout lub Procentframelayout z procent Biblioteki Supoort

<android.support.percent.PercentFrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
     <TextView
          android:layout_width="match_parent"
          app:layout_heightPercent="68%"/>
     <Gallery 
          android:id="@+id/gallery"
          android:layout_width="match_parent"
          app:layout_heightPercent="16%"/>
     <TextView
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_width="match_parent"/>
</android.support.percent.PercentFrameLayout>
 18
Author: rahulrv,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-10-01 21:57:57

Powyższy problem można również rozwiązać za pomocą ConstraintLayout za pomocą wytycznych.

Poniżej fragment.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.constraint.Guideline
    android:id="@+id/upperGuideLine"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.68" />

<Gallery
    android:id="@+id/gallery"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintBottom_toTopOf="@+id/lowerGuideLine"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/upperGuideLine" />

<android.support.constraint.Guideline
    android:id="@+id/lowerGuideLine"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.84" />

</android.support.constraint.ConstraintLayout>
 7
Author: Ujwal Agrawal,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-02-26 14:56:48

Spójrz na to:

Http://developer.android.com/reference/android/util/DisplayMetrics.html

Możesz uzyskać wysokość ekranu i to prosta matematyka obliczyć 68 procent ekranu.

 1
Author: Finn Larsen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-09-21 20:38:16