2012-04-18 3 views
0

기본적으로 활동은 화면에 표시되어야합니다.
1. 로고가 맨 위에 표시됩니다. 반드시 표시해야합니다. 선택 사항이 화면에 여분의 공간을 남겨 둘 경우 세로 및 가로 모두 가운데에 있어야합니다.
2. 하단의 ScrollView. 호스트는 사용자가 선택할 수있는 일련의 선택 사항을 호스트하는 세로 형 LinearLayout을 호스팅합니다. 모든 선택 사항을 표시하는 데 필요한 최소한의 공간을 차지해야합니다 (로고가 세로로 배치 됨). 화면에서 (화면 - 로고) 공간까지 차지할 수 있습니다. 선택 사항은 프로그래밍 방식으로 추가됩니다.ScrollView가 내장 된 RelativeLayout에서 일종의 순환 종속성

이제



, 나는대로 레이아웃을 정의 :

<?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. 이미지 뷰를 사용하여 누워 경우 layout_above가있는 ScrollView,있는 ScrollView는 이미지 뷰를 고려하여 최소 크기를 고려하지 전에 밖으로 누워있다 = .
  2. layout_below = ImageView를 사용하여 ScrollView를 레이아웃하면 ImageView가 맨 위에 머물러 있습니다. 단 2 ~ 3 가지 선택이있을 때 화면이보기 흉하게 보입니다.

    제약 조건을 충족시킬 수있는 방법이 있습니까? 아니면 두 개의 서로 다른 레이아웃 xml 파일을 정의하고 프로그래밍 방식으로 올바른 레이아웃으로 전환해야합니까?
+0

은'ScrollView' 없애보십시오. "모든 선택 사항"을 표시해야하는 경우 스크롤 할 내용이 없습니다. – CommonsWare

+0

ScrollView를 제거 할 수 없습니다. 나는 모든 선택을 즉시 표시해야한다고 말하지 않았다. 화면에 맞을 수 있다면 ScrollView는 ImageView를 중심으로 가능한 한 작은 공간을 차지해야합니다. 그렇지 않으면 ScrollView가 화면에서 (ParentContainer - ImageView) 높이까지 가져와야합니다. –

답변

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> 

이 나를 위해 작동하는 것 같다. 있는 ScrollView

+0

글쎄, 로고를 표시하는 단추가 하나 밖에없는 경우에도 로고가 위에 머물러 있습니다. –

+0

예. 나는 하나를 제외하고 모두를 꺼내었다. 그리고 그것은 아직도 거기에 머물러있다. 그것 자체를 중심으로 만들기 위해 javacode가 있어야합니다. – Storm2010

+0

아마 javacode가 true % P입니다. 나는 ScrollView를 검사하는 View.post (Runnable 작업)'을 수행했으며, 필요한 경우 LinearLayout 자식 높이를 즉시 교체하고 대체 레이아웃으로 전환합니다. 추악한 해결 방법을 없애기위한 제안을 아직 열어두고 있습니다. –

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' 태스크를 실행할 때 사용할 수 있습니다. 큐에있는 다른 모든 작업 (예 :로드 된 이미지, 모든 뷰가 올바르게 배치되고 크기가 조정 된 경우)이 수행 된 후에 작업이 실행됩니다. –

+0

'super.onCreate()'메소드 외부의 뷰를 측정하려고 시도합니다. [http} : //stackoverflow.com/questions/2159320/how-to-size-an-android-view-based-on-its-parents-dimensions) 사례 명세서를 적용하십시오. – Storm2010