LinearLayout의 백분율 너비를 정의 하시겠습니까? [복제]
이 질문에는 이미 답변이 있습니다.
일부 버튼이 포함 된 LinearLayout의 백분율 너비 (70 %)를 정의하여 가운데 버튼을 사용하여 하위 버튼을 채울 수 있도록하고 싶습니다. 다음은 내가 무슨 뜻인지 보여주는 그림입니다.
내 현재 레이아웃은 다음과 같습니다
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/layoutContainer" android:orientation="vertical">
<LinearLayout android:layout_width="fill_parent"
android:id="@+id/barContainer" android:orientation="horizontal"
android:layout_height="40dp" android:background="@drawable/titlebackground">
<ImageView android:id="@+id/barLogo" android:src="@drawable/titlelogo"
android:layout_gravity="center_vertical" android:adjustViewBounds="true"
android:layout_height="25dp" android:layout_width="wrap_content"
android:scaleType="fitXY" android:paddingLeft="5dp"></ImageView>
</LinearLayout>
<TextView android:layout_height="wrap_content"
android:layout_width="fill_parent" android:gravity="center_horizontal"
android:id="@+id/searchTip" android:text="@string/searchTip"
android:paddingTop="10dp" android:paddingBottom="10dp"></TextView>
<LinearLayout android:layout_height="wrap_content"
android:id="@+id/linearLayout1" android:orientation="vertical" android:layout_width="wrap_content">
<Button android:text="Button" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:layout_width="wrap_content" android:id="@+id/button2" android:layout_height="wrap_content" android:text="Button"></Button>
<Button android:layout_width="wrap_content" android:id="@+id/button3" android:layout_height="wrap_content" android:text="Button"></Button>
</LinearLayout>
</LinearLayout>
참조하는 LinearLayout im은 id : linearLayout1을 갖습니다. 어떻게해야합니까?
요소의 weight 속성을 설정해야합니다. LinearLayout의 하위 항목으로 3 개의 RelativeLayouts를 작성하고 가중치를 0.15, 0.70, 0.15로 설정하십시오. 그런 다음 두 번째 RelativeLayout (무게가 0.70 인 단추)에 단추를 추가하십시오.
이처럼 :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/layoutContainer" android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="0.15">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="0.7">
<!-- This is the part that's 70% of the total width. I'm inserting a LinearLayout and buttons.-->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<Button
android:text="Button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</LinearLayout>
<!-- 70% Width End-->
</RelativeLayout>
<RelativeLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="0.15">
</RelativeLayout>
</LinearLayout>
가중치가 0.15, 0.7 및 0.15 인 이유는 무엇입니까? 총 무게는 1이고 0.7은 총 70 %입니다.
결과:
편집 : 방향이 수직이 아니라 수평이어야한다는 것을 지적 한 @SimonVeloper 및 가중치 대신 정수가 아닌 소수를 지적하는 @Andrew에게 감사드립니다.
이것이 도움이되기를 바랍니다.
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal">
<LinearLayout android:layout_width="0dip"
android:layout_height="wrap_content" android:orientation="horizontal"
android:id="@+id/linearLayout_dummy1" android:layout_weight=".15">
</LinearLayout>
<LinearLayout android:layout_height="wrap_content"
android:id="@+id/linearLayout1" android:orientation="vertical"
android:layout_width="0dip" android:layout_weight=".7">
<Button android:text="Button" android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center">
</Button>
<Button android:layout_width="wrap_content" android:id="@+id/button2"
android:layout_height="wrap_content" android:text="Button"
android:layout_gravity="center"></Button>
<Button android:layout_width="wrap_content" android:id="@+id/button3"
android:layout_height="wrap_content" android:text="Button"
android:layout_gravity="center"></Button>
</LinearLayout>
<LinearLayout android:layout_width="0dip"
android:layout_height="wrap_content" android:orientation="horizontal"
android:id="@+id/linearLayout_dummy2" android:layout_weight=".15">
</LinearLayout>
</LinearLayout>
(1) layout_width를 "0dip"로 설정 (2) layout_height를 .xx (원하는 %)로 설정
무게와 작동하는 다른 솔루션을 알고 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:gravity="center_horizontal">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="7">
</LinearLayout>
</LinearLayout>
에미 암의 접근 방식이 옳다고 생각합니다. 그러나 Simon Veloper (Emima의 답변에 대한 해설자 중 하나) 와도 동의합니다 .70 % 너비의 버튼이 필요할 때 Linearlayout에 가로 방향을 사용하고 각 Relativelayout의 너비를 0dip 및 지정된 가중치로 설정해야합니다. 버전 :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/layoutContainer" android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="3">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="14">
//This is where you add buttons. You can make them "fill_parent".
</RelativeLayout>
<RelativeLayout
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="3">
</RelativeLayout>
</LinearLayout>
2015 년 Android에 대한 최신 업데이트로서 Google은 지원 라이브러리 비율을 포함했습니다
com.android.support : 퍼센트 : 23.1.0
사용 예를 들어이 사이트를 참조 할 수 있습니다
https://github.com/JulienGenoud/android-percent-support-lib-sample
Gradle:
dependencies {
compile 'com.android.support:percent:22.2.0'
}
In the layout:
<android.support.percent.PercentRelativeLayout
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">
<View
android:id="@+id/top_left"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:background="#ff44aacc"
app:layout_heightPercent="20%"
app:layout_widthPercent="70%" />
<View
android:id="@+id/top_right"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/top_left"
android:background="#ffe40000"
app:layout_heightPercent="20%"
app:layout_widthPercent="30%" />
<View
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/top_left"
android:background="#ff00ff22"
app:layout_heightPercent="80%" />
</android.support.percent.PercentRelativeLayout>
Use new percentage support library
compile 'com.android.support:percent:24.0.0'
See below example
<android.support.percent.PercentRelativeLayout
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">
<ImageView
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
app:layout_marginTopPercent="25%"
app:layout_marginLeftPercent="25%"/>
</android.support.percent.PercentRelativeLayout>
For More Info Tutorial1 Tutorial2 Tutorial3
You can't define width/height/margins/... using percents in your XML. But what you would want to use is the "weight" attribute, which is, IMO, the most similar thing.
Another method would be to set the sizes programmatically after you inflate the layout in your code, by getting the size of your screen and calculating needed margins.
*You can use layout_weight
properties. If you want to take 70% of parent width you must set child layout_width
property value 0dp, like android:layout_width="0dp"
*
I solved a similar issue applying some padding to the LinearLayout like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/app_background"
android:padding="35dip">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/black">
</RelativeLayout>
</LinearLayout>
이것은 아마도 정확한 비율을 제공하지는 않지만 쉽게 눈금을 매기고 불필요한 레이아웃 요소를 피할 수 있습니다.
구글은 PercentRelativeLayout 이라는 새로운 API를 도입 했습니다
다음과 같은 컴파일 종속성 추가
'com.android.support:percent:22.2.0'컴파일
그 PercentRelativeLayout에서 우리는 백분율 레이아웃을 할 수 있습니다
참고 URL : https://stackoverflow.com/questions/6557220/defining-a-percentage-width-for-a-linearlayout
'Programming' 카테고리의 다른 글
팬더는 평균 / 평균을 얻는다 (0) | 2020.07.18 |
---|---|
rsync와 함께 자격 증명 파일을 어떻게 사용합니까? (0) | 2020.07.18 |
LaTeX 옵션 인수 (0) | 2020.07.18 |
CRC가 MD5 / SHA1보다 사용하기에 더 적합한시기는 언제입니까? (0) | 2020.07.18 |
jQuery가 요소를 찾지 못했는지 확인 (0) | 2020.07.18 |