2016-12-19 1 views
0

버튼이있는 세로 형 LinearLayout이 있습니다. 이 버튼을 LinearLayout 너비의 70 %로 만들고 중심에 배치하고 싶습니다.안드로이드는 vertical_width에서 layout_weight로 설정합니다. LinearLayout

layout_width을 0으로 설정하는 동안 layout_weight을 0.7로 설정했지만 수직선 인 LinearLayout이므로 너비에는 영향을 미치지 않지만 높이에만 영향을 미칩니다.

세로 레이아웃에서 너비를 변경하려면 어떻게해야합니까? 버튼의 중첩 수평 LinearLayout을 추가해야만 그 무게를 설정할 수 있습니까?

+0

XML 코드를 보내주십시오. –

+0

및 기타 30 % 레이아웃에서는 '0dp'너비와 '0.3'가중치를 지정해야합니다. – Ironman

+0

귀하의 XML 코드를 게시하십시오. – Noorul

답변

2

새로운 PercentRelativeLayout을 사용해보세요. 중첩 된 레이아웃을 사용하지 않고 시도해 볼 수 있습니다.

는 의존성이 포함됩니다 com.android.support:percent:25.1.0

그리고 당신의 코드에서

<android.support.percent.PercentRelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    ... 

<Button 
     app:layout_widthPercent="70%" 
     android:layout_height="wrap_content"/> 
    ... 

</android.support.percent.PercentRelativeLayout> 
+0

선형 비율 레이아웃이 있습니까? –

+0

아니야하지만 그게 문제 야. –

+0

위젯을 다른 위젯 아래로 말할 수는 있겠지만 나는 그것을 피하려고 노력하고있다. 그리고 레이아웃을 linearlayout과 같이 설정하게한다. –

0
linearLayout -> orientation = horizontal 
button -> layout width = 0dp 
     -> layout height = wrap_content 
     -> layout weight = 1 
+0

가로형 레이아웃이 아닌 가로형 레이아웃이 필요합니다 –

+0

버튼의 너비를 match_parent로 설정하고 왼쪽 및 오른쪽 여백을 추가하거나 중첩 된 레이아웃을 사용하여 시도 할 수 있습니다 –

+0

설정을 수행합니까? 이 같은 마진은 양쪽 모두 좋은 연습으로 간주됩니까? –

0

이를 확인하시기 바랍니다.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="0.7" 
    android:gravity="center" 
    android:orientation="vertical"> 
    <Button 
     android:id="@+id/yourRequiredButton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
    </LinearLayout> 

    <Button 
     android:id="@+id/container" 
     android:layout_weight="0.3" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" /> 

</LinearLayout> 
관련 문제