2014-01-21 7 views
1

프로그래밍 방식으로 내 ScrollView에 일부보기를 추가하려고합니다. 여기에 XML 코드를 추가하려고합니다. 이것은Android 부드럽게 맨 아래로 스크롤

<RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="70dp" 
      android:padding="10dp"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_centerVertical="true" 
       android:text="1. Sadlier ofxord " 
       android:textColor="@color/text_color" 
       android:textSize="16sp" /> 

      <ImageButton 
       android:contentDescription="@string/hello_world" 
       android:layout_alignParentRight="true" 
       android:layout_width="50dp" 
       android:layout_height="50dp" 
       android:layout_centerVertical="true" 
       android:background="@drawable/delete_button" 
       android:src="@drawable/delete_icon" /> 



     </RelativeLayout> 
     <View 
      android:layout_width="fill_parent" 
      android:layout_height = "2dp" 
      android:background="@color/text_color"/> 

이 기능은 뷰를 만들 수평 선형 레이아웃에 기록

private RelativeLayout createContainerLayout(){ 
    RelativeLayout layout = new RelativeLayout(getApplicationContext()); 
    layout.setPadding(10, 10, 10, 10); 
    RelativeLayout.LayoutParams params = new  RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 70); 
    layout.setLayoutParams(params); 
    return layout; 
} 

private ImageButton createDeleteImageButton(){ 
    ImageButton button = new ImageButton(getApplicationContext()); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50); 
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    params.addRule(RelativeLayout.CENTER_VERTICAL); 
    button.setLayoutParams(params); 
    button.setBackgroundResource(R.drawable.delete_button); 
    button.setImageResource(R.drawable.delete_icon); 
    return button; 
} 

private TextView createSetNameText(int counter , String name){ 
    TextView text = new TextView(getApplicationContext()); 
    text.setText(counter+". "+name); 
    text.setTextSize(16); 
    text.setTextColor(getResources().getColor(R.color.text_color)); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
    params.addRule(RelativeLayout.CENTER_VERTICAL); 
    return text; 
} 

private View createLineView(){ 
    View line = new View (getApplicationContext()); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT , 2); 
    line.setLayoutParams(params); 
    line.setBackgroundColor(getResources().getColor(R.color.text_color)); 
    return line; 
} 

이 내가있는 ScrollView

lLayoutContainer.addView(rLayout); 
rLayout.addView(setName); 
rLayout.addView(deleteButton); 
lLayoutContainer.addView(line); 

에보기를 추가하지만 뭔가를 얻을 방법입니다 결국. 첫 번째 행은 XML에 의해 작성된 행이며, 다음 두 행은 프로그래밍 방식으로 작성됩니다. 그래서 내 코드에 어떤 문제가 있습니까? 이 작은 높이가 양쪽 모두 setPadding를 들어

enter image description here

답변

1

LayoutParams 생성자와 레이아웃을 생성 왜, 당신은 크기에 명시 적으로 값을 전달한다. 이것은 화면 밀도와 관계없이 절대 픽셀 크기입니다.

대신 getDimensionPixelOffset 또는 getDimensionPixelSize을보십시오. 예 :

private RelativeLayout createContainerLayout(){ 
    final Resources r = getResources(); 
    final int tenDp = r.getDimensionPixelSize(R.dimens.my_padding); 
    RelativeLayout layout = new RelativeLayout(getApplicationContext()); 
    layout.setPadding(tenDp, tenDp, tenDp, tenDp); 

    final int seventyDp = r.getDimensionPixelSize(R.dimens.my_height); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, seventyDp); 
    layout.setLayoutParams(params); 
    return layout; 
} 
+0

대단히 감사합니다. – user3199577

관련 문제