2014-01-10 24 views

回答

8

使用父爲LinearLayout和使用weightSumweight屬性

樣品

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:weightSum="100" > 

    <RelativeLayout 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="50" > 

     <!-- Your RelativeLayout Items --> 

    </RelativeLayout> 

</LinearLayout> 
0
*@SuppressLint("NewApi") 
public static int getDeviceWidth(Activity activity) { 
    int deviceWidth = 0; 

    Point size = new Point(); 
    WindowManager windowManager = activity.getWindowManager(); 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     windowManager.getDefaultDisplay().getSize(size); 
     deviceWidth = size.x; 
    } else { 
     Display display = windowManager.getDefaultDisplay(); 
     deviceWidth = display.getWidth(); 
    } 
    return deviceWidth; 
} 

@SuppressLint("NewApi") 
public static int getDeviceHeight(Activity activity) { 
    int deviceHeight = 0; 

    Point size = new Point(); 
    WindowManager windowManager = activity.getWindowManager(); 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     windowManager.getDefaultDisplay().getSize(size); 
     deviceHeight = size.y; 
    } else { 
     Display display = windowManager.getDefaultDisplay(); 
     deviceHeight = display.getHeight(); 
    } 
    return deviceHeight; 
}* 

上述兩個功能是獲取設備的高度和寬度。

使用此功能,可以獲得屏幕大小的一半。
我希望它對你有用。

1

你需要把RelativeLayout一個LinearLayout內使用android:weightsum=2android:layout_weight=1以獲得所需的比例

<?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="horizontal" 
    android:weightSum="2" > 

    <RelativeLayout 
     .... 
     android:layout_weight=1 
     ....> 
    </RelativeLAyout> 

</LinearLayout> 
2

劃分的相對佈局成兩個相等的相對佈局

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:baselineAligned="false"> 
<RelativeLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 
</RelativeLayout> 
<RelativeLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 
</RelativeLayout> 
</RelativeLayout> 

和分割的線性佈局成兩個相等的相對佈局

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" 
android:baselineAligned="false"> 
<RelativeLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 
</RelativeLayout> 
<RelativeLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 
</RelativeLayout> 
</LinearLayout> 
+0

我還沒有嘗試過第一種方法,但人們對將LinearLayout作爲解決方案提出了瘋狂的建議。我會給你一個鏡頭。感謝分享。 – Diolor

+0

好吧@Diolor .. :) – Lal

相關問題