2014-01-21 104 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; 
} 

,這是我怎麼加的意見,以滾動型

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

但我得到的東西像這到底。第一行是由XML創建的行,接下來的兩行是programaticaly創建的。那麼我的代碼有什麼問題?爲什麼它具有較小的高度

enter image description here

回答

1

對於這兩種setPaddingLayoutParams構造函數創建一個佈局,你傳遞明確的值的大小。無論屏幕密度如何,這將是絕對像素大小。

請轉到getDimensionPixelOffsetgetDimensionPixelSize。例如:

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