2013-02-22 41 views
0

我有一個機器人應用程序,它具有大的表圖像的設計(參見A,下文B畫面) 這意味着我已經有A和B與所有尺寸的圖像我需要(mdpi,hdpi,xhdpi): enter image description here極限滾動型/列表高度

我的問題是AI內部需要放置一個scrollview或項目列表,但我不希望它們大於A圖像。 我目前的佈局是類似如下:

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

    <RelativeLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 

     // Some elements here 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 

     // Some elements here 

    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_gravity="center_horizontal"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/A_background"/> 

     <ScrollView 
      android:id="@+id/scrollview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <LinearLayout 
       android:id="@+id/scrollview_container" 
       android:orientation="vertical" 
       android:gravity="center_horizontal" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_margin="5dp"/> 
     </ScrollView> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_gravity="center_horizontal"> 

     <ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/B_background"/> 
    </RelativeLayout> 

</LinearLayout> 

當我的滾動視圖有很多的項目,A正在擴大,我需要一個像以前那樣由於圖像漸漸模糊是相同的高度。

回答

1

在這些情況下,我通常最終編寫了一個自定義視圖,用MeasureSpec.UNSPECIFIED測量一個視圖(在本例中爲ImageView),另一個(在您的情況下爲ScrollView)使用MeasureSpec.EXACTLY或MeasureSpec.AT_MOST取決於你的需求)。 東西沿着線:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int width = MeasureSpec.getSize(widthMeasureSpec); 
    int height = MeasureSpec.getSize(heightMeasureSpec); 

    // measure view 1 
    View view1 = findViewById(R.id.view1); 
    view1.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.UNSPECIFIED)); 
    int view1Height = view1.getMeasuredHeight(); 

    // check measured height against MeasureSpec and adapt accordingly 
    int heightMsrSpec = MeasureSpec.getMode(heightMeasureSpec); 
    switch (heightMsrSpec) { 
    case MeasureSpec.AT_MOST: view1Height = Math.min(height, view1Height); break; 
    case MeasureSpec.EXACTLY: view1Height = height; break; 
    case MeasureSpec.UNSPECIFIED: break; 
    } 

    // measure view2 
    View view2 = findViewById(R.id.view2); 
    view2.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(view1Height, MeasureSpec.EXACTLY)); 
    int view2Height = view2.getMeasuredHeight(); 

    setMeasuredDimension(width, Math.min(view1Height, view2Height) + getPaddingTop() + getPaddingBottom()); 
} 
+0

我不明白的最後一部分,如何我可以通過正確的高度,當我從我的xml使用這個自定義視圖?它有wrap_content,因爲我不知道它的高度。 – shayy 2013-02-22 13:08:31

+0

自定義視圖及其家長有Android:layout_height標籤爲好。根據這些標籤的值,onMeasure()方法需要以不同的方式工作。我在示例中添加了必要的代碼行。 – 2013-02-22 13:45:38

+0

你的情況的問題是,RelativeLayout的已WRAP_CONTENT的高度,並同時與滾動型和ImageView的,但滾動型的增長應該不會比ImageView的高。除非您將ScrollView的高度設置爲固定值,否則無法在XML中配置此需求。因此,自定義視圖看起來更加複雜,但一旦你第一次完成它就很容易。我遇到過許多情況,其中自定義視圖可以輕鬆滿足我的需求,而XML無法滿足需求。 – 2013-02-22 13:53:39

0

以及A爲背景,形象是你確定這是正確的XML文件可以不加內imageview的滾動視圖,它不是在它裏面要麼

你可以做的就是創建一個像

不同RES /佈局文件夾
res/layout-small 
res/layout-large 
res/layout-xlarge 
res/layout-sw600 
res/layout-sw720 

將您的佈局文件放在其中,並根據您對他們的圖像給予絕對高度A & B.

這樣,它不會拉伸,並期待在每一個屏幕尺寸較好。

+0

滾動視圖不是ImageView的裏面,它的RelativeLayout的裏面,我無法避免這些imageviews因爲分配我的A,B圖像作爲背景,以relativelayouts也舒展他們 – shayy 2013-02-22 12:54:02

+0

此外,如何避免在使用不同的佈局文件夾時複製我的所有xml佈局? – shayy 2013-02-22 12:54:38

+0

你無法避免它,如果你需要多屏幕支持和應用圖像爲背景,以相對佈局和分配hieght和寬度佈局不會拉伸圖像和佈局 – 2013-02-22 12:56:25