2012-06-16 19 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()方法。基本上你可以在每个你想要的组件的末尾添加一行。 –