내 안드로이드 앱에서 로그인 '활동'에 대한 양식 레이아웃을 작업하고 있습니다. 아래 이미지는 제가 원하는 모습입니다:
다음 XML을 사용하여 이 레이아웃을 얻을 수 있었습니다. 문제는 약간 해킹이 필요하다는 것입니다. 호스트 EditText의 너비를 하드코딩해야 했습니다. 구체적으로 말하자면, 지정해야 했습니다:
android:layout_width="172dp"
호스트와 포트 EditText에 너비를 백분율로 지정하고 싶습니다. (호스트는 80%, 포트는 20% 정도) 이것이 가능한가요? 다음 XML은 내 Droid에서 작동하지만 모든 화면에서 작동하지는 않는 것 같습니다. 좀 더 강력한 솔루션을 원합니다.
<!-- begin snippet: js hide: false -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/host_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/home"
android:paddingLeft="15dp"
android:paddingTop="0dp"
android:text="host"
android:textColor="#a5d4e2"
android:textSize="25sp"
android:textStyle="normal" />
<TextView
android:id="@+id/port_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/home"
android:layout_toRightOf="@+id/host_input"
android:paddingTop="0dp"
android:text="port"
android:textColor="#a5d4e2"
android:textSize="25sp"
android:textStyle="normal" />
<EditText
android:id="@+id/host_input"
android:layout_width="172dp"
android:layout_height="wrap_content"
android:layout_below="@id/host_label"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="4dp"
android:background="@android:drawable/editbox_background"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/port_input"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="@id/host_label"
android:layout_marginTop="4dp"
android:layout_toRightOf="@id/host_input"
android:background="@android:drawable/editbox_background"
android:inputType="number" />
<TextView
android:id="@+id/username_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/host_input"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:text="username"
android:textColor="#a5d4e2"
android:textSize="25sp"
android:textStyle="normal" />
<EditText
android:id="@+id/username_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/username_label"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="4dp"
android:background="@android:drawable/editbox_background"
android:inputType="textEmailAddress" />
<TextView
android:id="@+id/password_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/username_input"
android:paddingLeft="15dp"
android:paddingTop="15dp"
android:text="password"
android:textColor="#a5d4e2"
android:textSize="25sp"
android:textStyle="normal" />
<EditText
android:id="@+id/password_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/password_label"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="4dp"
android:background="@android:drawable/editbox_background"
android:inputType="textPassword" />
<ImageView
android:id="@+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="false"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingTop="15dp"
android:scaleType="fitStart"
android:src="@drawable/home" />
<Button
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/password_input"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:text=" login "
android:textSize="18sp" >
</Button>
</RelativeLayout>
<!-- end snippet -->
안드로이드:레이아웃_무게` 속성을 찾고 있습니다. 이 속성을 사용하면 백분율을 사용하여 레이아웃을 정의할 수 있습니다.
다음 예제에서 왼쪽 버튼은 공간의 70%를 사용하고 오른쪽 버튼은 30%를 사용합니다.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".70" />
<Button
android:text="right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".30" />
</LinearLayout>
모든 종류의 보기에서 동일하게 작동하며, 필요에 맞게 버튼을 일부 편집 텍스트로 바꿀 수 있습니다.
레이아웃 너비를
0dp`로 설정하지 않으면 뷰의 크기가 제대로 조정되지 않을 수 있습니다.
가중치 합계가 반드시 1이어야 하는 것은 아니며, 이렇게 설정하는 것이 더 읽기 편합니다. 첫 번째 가중치를 7로 설정하고 두 번째 가중치를 3으로 설정해도 동일한 결과를 얻을 수 있습니다.
원래 질문은 꽤 답변할 수 있는 것은 아니며, 그러나 특별한 경우 70/30 노트북이었습니다 분할합니다 구성 요소 사이의 분할줄 50/50 길이 있다. 보이지 않는 중심에 받침대 위에 두 개의 요소이라고 포지셔닝해야 迈向 사용하고 있다.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View android:id="@+id/strut"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignRight="@id/strut"
android:layout_alignParentLeft="true"
android:text="Left"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/strut"
android:layout_alignParentRight="true"
android:text="Right"/>
</RelativeLayout>
이를 매우 일반적인 경우, 이 솔루션은 이상 관심의 대상이 되었다. 하지만 한 것은 조금 크기가 0 인 받침대 비용 때문에 매우 효과적인 비어 있는 해킹 /dev/raw/raw200 리틀이에요.
하지만 너무 큰 성과를 기대하지 않는 것이 일반적으로, s it& # 39 에서 안드로이드 스톡과 레이아웃.
아래 구글 comment:* 인용
>. 이 API 는 클래스 수준 26.0.0 더 이상 사용되지 않습니다. 콘스트라인트레이우스 및 관련 레이아웃 대신 사용하는 것이 좋습니다. 다음과 같은 기능을 활용할 수 있는 방법을 보여 replicate 콘스트라인트레이우스 레이아웃을 비율 (%)
그런 다음 뷰 (view) 에 의해 시행하십시오 그냥 대체하십시오 비율 (%)
컴파일하십시오 상관 관계 등 추가
compile 'com.android.support:percent:22.2.0
그 것이 우리가 할 수 있는 현명한 레이아웃뿐만 비율 (%), 페르센트렐라티벨리우스
<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>
페르센트렐라티벨리우스 사용할 수 있는 것이 아니라 최근 문서화되지 않은 디자인 지원 가능 대체하십시오 라이브러리란 , 요소 뿐만 아니라 상대적 전체 백분율을 사용할 수 있는 공간.
>. 레이티벨리우스 敲阀汽俊 汲摹窍妨绰 비율 (%) 의 하위 클래스 기반 치수에는 >. 및 여유폭. 자식 하나 또는 차로 사용하여 지정할 수 있습니다. >. 속성이 " Percent"; 접미어입니다.
<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
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
app:layout_marginTopPercent="25%"
app:layout_marginLeftPercent="25%"/>
</android.support.percent.PercentFrameLayout>
퍼센트 (비율) api 를 지원할 수 있는 패키지를 이용하면 추가 및 관리 기반 치수에는 in your app.
dependencies {
compile 'com.android.support:percent:22.2.0'//23.1.1
}
>. >. 의해 abc. @EmJiHash 페르센트렐라티벨리우스 및 페르첸시오프라메라리우스 있다 >. >. api 는 26.0.0 에서 사용되지 않는 수준 >. >. 콘스트라인트레이우스 사용하는 것이 좋습니다
Google 은 새로운 apiu 불렀으매 안드로이드 자주포트스페르센트
1) 페르센트렐라티벨리우스
2) 페르첸시오프라메라리우스
컴파일하십시오 상관 관계 등 추가
compile 'com.android.support:percent:23.1.1'
퍼센트 (비율) 에서 모두 활용할 수 있어 '와' 레이티벨리우스 치수처리 지정할 수 있습니다.
<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"/>
<TextView
app:layout_widthPercent="40%"
app:layout_heightPercent="40%"
app:layout_marginTopPercent="15%"
app:layout_marginLeftPercent="15%"/>
</android.support.percent.PercentRelativeLayout/>
나는 이 만드는 것은 사용자 정의 보기:
public class FractionalSizeView extends View {
public FractionalSizeView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public FractionalSizeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = MeasureSpec.getSize(widthMeasureSpec);
setMeasuredDimension(width * 70 / 100, 0);
}
}
이는 다른 뷰를 정렬하려면 보이지 않는 받침대 레이티벨리우스 내에서 사용할 수 있습니다.
이후 당시 26.0.0 페르센트렐라티벨리우스 에서 사용되지 않는 레이아웃 및 네스트된 린얼리우스 δ1 레이티벨리우스 같은 부정적인 영향을 미칠 성능 ([이해를 통해 성능을 콘스트라인트레이우스] (https://android-developers.googleblog.com/2017/08/understanding-performance-benefits-of.html)) 의 비율 (옵션을 달성할 수 있는 최고의 너버 콘스트라인트레이우스 레이티벨리우스 교체하는 것입니다.
이 두 가지 방법으로 해결할 수 있다.
<android.support.constraint.ConstraintLayout
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">
<TextView
android:id="@+id/host_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Host"
android:layout_marginTop="16dp"
android:layout_marginLeft="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="@+id/host_input" />
<TextView
android:id="@+id/port_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Port"
android:layout_marginTop="16dp"
android:layout_marginLeft="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="@+id/port_input" />
<EditText
android:id="@+id/host_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:inputType="textEmailAddress"
app:layout_constraintTop_toBottomOf="@+id/host_label"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/guideline" />
<EditText
android:id="@+id/port_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:inputType="number"
app:layout_constraintTop_toBottomOf="@+id/port_label"
app:layout_constraintLeft_toLeftOf="@+id/guideline"
app:layout_constraintRight_toRightOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.8" />
</android.support.constraint.ConstraintLayout>
<android.support.constraint.ConstraintLayout
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">
<TextView
android:id="@+id/host_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Host"
android:layout_marginTop="16dp"
android:layout_marginLeft="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="@+id/host_input" />
<TextView
android:id="@+id/port_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Port"
android:layout_marginTop="16dp"
android:layout_marginLeft="8dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="@+id/port_input" />
<EditText
android:id="@+id/host_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:inputType="textEmailAddress"
app:layout_constraintHorizontal_weight="0.8"
app:layout_constraintTop_toBottomOf="@+id/host_label"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/port_input" />
<EditText
android:id="@+id/port_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:inputType="number"
app:layout_constraintHorizontal_weight="0.2"
app:layout_constraintTop_toBottomOf="@+id/port_label"
app:layout_constraintLeft_toRightOf="@+id/host_input"
app:layout_constraintRight_toRightOf="parent" />
</android.support.constraint.ConstraintLayout>
두 경우 모두 이 같은 일이 확보하십시오
1 에서 [페르센트렐라티벨리우스] 는 사용되지 26.0.0 개전판이 의 지원 라이브러리.
이에 따라 해당 파일 라이브러리 의존성을 부ild.그래들 추가 모듈 수준.
dependencies {
compile 'com.android.support.constraint:constraint-layout:1.0.1'
}
레이아웃에 추가하기만 파일:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.constraint.ConstraintLayout>
구속 도움말에서는 위젯 정렬되고 보관할 수 있습니다. 아래와 같은 제약조건 처리, 앵커, 사용할 수 있는 다양한 맞춤에는 경계선 결정하는 것이 일반적이다.
이 관계자는 doc 에서 자세한 내용을 확인할 수 있습니다.
흥미롭게도, 충분한 답을 할 수 있는 기반으로 한 @olefevre 뿐 아니라, 보이지 않는 struts" " 50/50 레이아웃, 레이아웃, 하지만 모든 권력을 신규인지 관련된 두 가지.
예를 들어, 그 다음은 4 개 분야로 비유하 너버 잘라냅니다 레이아웃용 부품 (실제로는 3 급지합니다 함께 1, 1, 2):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<View
android:id="@+id/strut"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:background="#000000" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/strut" >
<View
android:id="@+id/left_strut"
android:layout_width="1dp"
android:layout_height="match_parent"
android:layout_toLeftOf="@+id/strut"
android:layout_centerHorizontal="true"
android:background="#000000" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/left_strut"
android:text="Far Left" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/left_strut"
android:text="Near Left" />
</RelativeLayout>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/strut"
android:layout_alignParentRight="true"
android:text="Right" />
</RelativeLayout>
체크 (https://github.com/mmin18/FlexLayout) 에서 직접 사용할 수 있는 비율 또는 jave 표현식에서는 레이아웃용 xlm.
<EditText
app:layout_left="0%"
app:layout_right="60%"
app:layout_height="wrap_content"/>
<EditText
app:layout_left="prev.right+10dp"
app:layout_right="100%"
app:layout_height="wrap_content"/>