program story

UI 요소의 위치 / 크기를 화면 크기의 백분율로 설정

inputbox 2020. 11. 5. 07:59
반응형

UI 요소의 위치 / 크기를 화면 크기의 백분율로 설정


레이아웃을 만들 때 백분율 위치 / 크기를 사용할 수 있는지 알아 보려고합니다. 내가 원하는 건 이런 거 ...

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

가로로 800x480 실제 픽셀의 디스플레이가있는 장치에서 테스트 중이며 현재 다음과 같이 강제하고 있습니다.

<?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>

분명 내가 하드 코드로 고정하지 않을 px단위하지만 난 사용할 수없는 68%0.68에 대한 layout_marginTop(예를 들어). dp단위를 살펴 봤지만 나도 그렇게 할 수 있을지 모르겠다.

UI 디자인이 내 약점이라는 점을 인정해야하므로 어떤 조언도 고맙게 받아 들일 것입니다.

편집 : 누군가가 비슷한 대답을 찾고 있다면 Alan Moore의 제안에 따라 향후 참조를 위해 내가 원하는 방식으로 정확하게 작동합니다 ...

<?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>

나는 사용의 다른 예를 찾아서 높이를 layout_weight설정 하고 무게에 수레를 사용 하기로 결정했습니다 . 잘 작동합니다. :)TextView0dp


나는 당신이 원하는 것은 android : layout_weight를 설정하는 것이라고 생각합니다.

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

다음과 같은 것 (나는 단지 위아래에 자리 표시 자로 텍스트 뷰를 넣을 것입니다) :

  <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>

Percent Supoort 라이브러리 에서 PercentRelativeLayout 또는 PercentFrameLayout 사용

<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>

For TextView and it's descendants (e.g., Button) you can get the display size from the WindowManager and then set the TextView height to be some fraction of it:

Button btn = new Button (this);
android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
btn.setHeight((int)(display.getHeight()*0.68));

The above problem can also be solved using ConstraintLayout through Guidelines.

Below is the snippet.

<?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>

Take a look at this:

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

You can get the heigth of the screen and it's simple math to calculate 68 percent of the screen.

참고URL : https://stackoverflow.com/questions/7506230/set-position-size-of-ui-element-as-percentage-of-screen-size

반응형