2012-06-16 18 views
0

嘗試在我的視圖中創建黑線以分隔文本塊但未顯示。 文本顯示,因爲它應該,但我沒有看到該行。Android 2.3.3,在視圖中創建一條線

編輯: 已經測試添加動態按建議,也修改我的代碼,但仍然沒有線?我錯過了什麼嗎?

而且這是一個片段裏,類擴展片段{}

XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

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

</ScrollView> 

Java代碼:

public class Travel extends Fragment { 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    return inflater.inflate(R.layout.travel_fragment, container, false); 
} 


@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onViewCreated(view, savedInstanceState); 

LinearLayout layout = (LinearLayout)view.findViewById(R.id.travelContainer); 

     TextView text = new TextView(getActivity()); 
     int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,4, getActivity().getResources().getDisplayMetrics()); 
     text.setPadding(padding, padding, padding, padding); 
     text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12); 
     text.setTypeface(null, Typeface.BOLD); 
     text.setText("TITLE"); 
     text.setId(123456789); 
     layout.addView(text); 

     /* 
    View v = new View(getActivity()); 
    LinearLayout.LayoutParams viewLp = new LayoutParams(LayoutParams.FILL_PARENT,1); 
    viewLp.setMargins(0, 5, 0, 5); 
    v.setLayoutParams(viewLp); 
    v.setBackgroundColor(0x000); 
      */ 

    View v = getActivity().getLayoutInflater().inflate(R.layout.line, (ViewGroup)getActivity().getCurrentFocus(), false); 
    layout.addView(v); 


     text = new TextView(getActivity()); 
     padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,4, getActivity().getResources().getDisplayMetrics()); 
     text.setPadding(padding, padding, padding, padding); 
     text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);  
     text.setText("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."); 
     layout.addView(text); 
} 
} 

回答

1

你需要以編程方式創建視圖?

一個簡單的方法是創建具有以下屬性

<View 
    android:layout_width="fill_parent" 
    android:layout_height="1dp" 
    android:background="#000" /> 

視圖如果你創建了一個名爲line.xml你一個新的XML文件可以使用LayoutInflater獲得動態的觀點:

View line = MyActivity.this.getLayoutInflater().inflate(R.layout.line, (ViewGroup) getCurrentFocus(), false); 

系統爲您完成所有工作時,該版本將產生更簡潔的代碼。

+0

需要以編程方式執行它,因爲我添加了未知數量的視圖。這些項目來自數據庫。 – Patrick

+0

您仍然可以以編程方式膨脹該XML視圖並動態使用它。代碼將會更清潔。有關詳細信息,請參閱編輯。 – Tim

+0

嘗試過,但由於某種原因它仍然不打印行。 – Patrick

0

在您的佈局參數中,您必須將高度設置爲1,並將寬度設置爲填充父項。您只設置1的高度/寬度參數。請嘗試以下

LinearLayout.LayoutParams viewLp = new LayoutParams(LayoutParams.FILL_PARENT,1); 
viewLp.setMargins(0, 5, 0, 5); 

此外,您使用的LayoutParams一個RelativeLayout的但您使用的LinearLayout。 LinearLayouts不支持以下內容:

viewLp.addRule(RelativeLayout.BELOW, 123456789); 
viewLp.addRule(RelativeLayout.CENTER_HORIZONTAL); 

使用LinearLayout.LayoutParams。 LinearLayout將按照它們添加的順序來水平或垂直疊加視圖。

+0

試過但沒有區別。它仍然不打印一行? – Patrick

+0

什麼是你的背景顏色? – wdziemia

+0

白色,將線條改爲紫色以使其脫穎而出。 – Patrick

0

我做了類似的事情,我認爲和一個簡單的方法是覆蓋組件的類,特別是你想擁有行分隔符的onDraw()方法。

我想你的目標太在每個TextView的末尾添加一條黑線,所以你可以做到以下幾點:

1)創建一個PNG黑線

2)宣佈擴展的TextView類和overwrinting所述的onDraw方法和執行以下操作==>

private Bitmap line; 

然後在構造:

line = BitmapFactory.decodeResource(context.getResources(), R.drawable.line_thin); 
    line = Bitmap.createBitmap(line, 0, 0, this.getWidth(), 1); 

再上的onDraw()方法:

canvas.drawBitmap(line, 0, this.getHeight(), null); 

3),最後你只是不要忘了你在你的XML創建的類來改變你的組件的類型。

我不知道這個解決方案是否適合你,但我認爲這是一個好的方法,那麼每當你創建這個Textview的時候就會動態創建這一行。

希望它有幫助。

+0

問題是它並不總是一個textview。我也隨時添加圖像。也許然後創建一個繼承自TextView的類,但只打印該行而沒有其他內容。但是,那麼它有多大? 200像素? 1000像素? – Patrick

+0

我編輯了我的代碼:當我創建位圖時,它現在被設置爲具有組件寬度。爲了回答這個問題,這只是一個懶散的路線,它無論如何都適合。關於您的即時添加,您可以擴展一個/ many組件並覆蓋onDraw()方法。基本上你可以在每個你想要的組件的末尾添加一行。 –