2012-04-18 69 views
0

基本上,活動應該顯示在屏幕上:
1.頂部有一個徽標。必須始終顯示。當選擇在屏幕上留下額外的空間時,必須垂直和水平居中。
2.底部有一個ScrollView。主持垂直的LinearLayout,它接着託管一組用戶可以選擇的選擇。必須佔用最少的空間來顯示所有選項(所以徽標是垂直居中的)。可以佔用屏幕上的(屏幕 - 標識)空間。選擇以編程方式添加。Kinda在RelativeLayout中嵌入ScrollView的循環依賴



現在,我已經在佈局定義它:

<?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="match_parent"> 


     <ImageView 
      android:id="@+id/imageView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:padding="5dp" 
      android:src="@drawable/logo" /> 

     <ScrollView 
      android:id="@+id/scrollView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_below="@id/imageView" 
      > 

      <LinearLayout 
       android:id="@+id/ChoicesPanel" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" > 
      </LinearLayout> 
     </ScrollView> 
    </RelativeLayout> 
  1. 如果ImageView的是奠定了使用layout_above =滾動視圖,滾動型是之前奠定的,完全沒有考慮到的ImageView的代價最小尺寸。
  2. 如果使用layout_below = ImageView展開ScrollView,則ImageView會保持最佳狀態。當只有2-3個選擇時,屏幕看起來非常難看。

    有沒有辦法滿足約束條件?或者我應該定義兩個不同的佈局xml文件,並以編程方式切換到正確的文件?
+0

擺脫'ScrollView'的。如果「所有選項」必須可見,則不需要滾動。 – CommonsWare 2012-04-18 20:01:07

+0

無法擺脫ScrollView。我沒有說所有的選擇都必須立即顯示。如果它可以放在屏幕上,那麼ScrollView必須佔用儘可能小的空間,以ImageView爲中心。否則,ScrollView必須在屏幕上佔用(ParentContainer - ImageView)高度。 – 2012-04-18 21:22:49

回答

0
<?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="match_parent"> 


    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:padding="5dp" 
     android:src="@drawable/ic_launcher" /> 


    <ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/imageView" > 

     <LinearLayout 
      android:id="@+id/ChoicesPanel" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" > 

      <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_text_1"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_button_1"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_text_2"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_button_2"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_text_3"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_button_3"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_text_4"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_button_4"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_text_5"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_button_5"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_text_6"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_button_6"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_button_1"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_text_2"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_button_2"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_text_3"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_button_3"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_text_4"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_button_4"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_text_5"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_button_5"/> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_text_6"/> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="scroll_view_1_button_6"/> 


     </LinearLayout> 
    </ScrollView> 
</RelativeLayout> 

這似乎爲我工作。它在滾動視圖

+0

那麼,即使只有一個按鈕可以顯示,徽標也會保持在最前面,不是嗎? – 2012-04-18 19:25:34

+0

是的。我拿出所有東西,但仍然停留在那裏。讓它自己中心自己沒有什麼是你必須要javacode。 – Storm2010 2012-04-18 20:21:37

+0

可能是javacode是true%P。我做了一個'View.post(Runnable任務)',它檢查ScrollView和它的直接LinearLayout子高度,如果需要切換到備用佈局。仍然建議擺脫醜陋的解決方法。 – 2012-04-18 21:26:48

0
public class TestActivity extends Activity { 

private RelativeLayout mRelativeLayout; 
private RelativeLayout mRelativeLayout2; 
private ImageView mImageView; 
private ScrollView mScrollView; 
private TextView TV1; 
private TextView TV2; 
private TextView TV3; 
private TextView TV4; 
private Button B1; 
private Button B2; 
private Button B3; 
private Button B4; 
private RelativeLayout.LayoutParams lp; 


public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

mRelativeLayout = new RelativeLayout(this); 
    setContentView(mRelativeLayout); 

mImageView = new ImageView(this); 
mImageView.setId(1); 
RelativeLayout.LayoutParams lp = new  RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); 
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
mImageView.setImageResource(R.drawable.ic_launcher); 
mRelativeLayout.addView(mImageView, lp); 

mScrollView = new ScrollView(this); 
RelativeLayout.LayoutParams lp2 = new  RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
lp2.addRule(RelativeLayout.BELOW, mImageView.getId()); 
mRelativeLayout.addView(mScrollView, lp2); 

mRelativeLayout2 = new RelativeLayout(this); 
int itemcounts = mRelativeLayout2.getChildCount(); 
mScrollView.addView(mRelativeLayout2); 

TV1 = new TextView(this); 
TV1.setId(2); 
TV1.setTextSize(20); 
TV1.setText("Im a Text View. The First"); 
mRelativeLayout2.addView(TV1); 

B1 = new Button(this); 
B1.setId(3); 
RelativeLayout.LayoutParams bl1 = new  RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
bl1.addRule(RelativeLayout.BELOW, TV1.getId()); 
B1.setText("Im a Button. The First"); 
B1.setTextSize(25); 
mRelativeLayout2.addView(B1, bl1); 

TV2 = new TextView(this); 
RelativeLayout.LayoutParams tvl2 = new  RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
tvl2.addRule(RelativeLayout.BELOW, B1.getId()); 
TV2.setId(4); 
TV2.setTextSize(20); 
TV2.setText("Im a Text View. The Second"); 
mRelativeLayout2.addView(TV2, tvl2); 

B2 = new Button(this); 
B2.setId(5); 
RelativeLayout.LayoutParams bl2 = new  RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
bl2.addRule(RelativeLayout.BELOW, TV2.getI()); 
B2.setText("Im a Button. The Second"); 
B2.setTextSize(25); 
mRelativeLayout2.addView(B2, bl2); 

TV3 = new TextView(this); 
TV3.setId(6); 
RelativeLayout.LayoutParams tvl3 = new  RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
tvl3.addRule(RelativeLayout.BELOW, B2.getId()); 
TV3.setTextSize(20); 
TV3.setText("Im a Text View. The Third"); 
mRelativeLayout2.addView(TV3, tvl3); 

B3 = new Button(this); 
B3.setId(7); 
RelativeLayout.LayoutParams bl3 = new  RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
bl3.addRule(RelativeLayout.BELOW, TV3.getId()); 
B3.setText("Im a Button. The Third"); 
B3.setTextSize(25); 
mRelativeLayout2.addView(B3, bl3); 

TV4 = new TextView(this); 
TV4.setId(8); 
RelativeLayout.LayoutParams tvl4 = new  RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
tvl4.addRule(RelativeLayout.BELOW, B3.getId()); 
TV4.setTextSize(20); 
TV4.setText("Im a Text View. The Fourth"); 
mRelativeLayout2.addView(TV4, tvl4); 


if (itemcounts < 4) { 
    lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); 
    lp.addRule(RelativeLayout.CENTER_IN_PARENT); 
} 
if (itemcounts > 4) { 
    lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); 
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
} 

} 
} 

像14項試試這個

+0

'if(itemCounts <4)':)。不錯的嘗試。佈局完成後,最好依靠實際尺寸。運行發佈在根佈局上的'Runnable'任務時,可以使用實際大小。任務在隊列中的所有其他任務完成後執行,即圖像加載,所有視圖正確鋪設和大小等。 – 2012-04-20 06:04:40

+0

嘗試測量'super.onCreate()'方法之外的視圖[學習如何在此](http ://stackoverflow.com/questions/2159320/how-to-size-an-android-view-based-on-its-parents-dimensions)然後應用一個case語句 – Storm2010 2012-04-20 13:24:59