0

テキスト ブロックを区切るためにビューに黒い線を作成しようとしていますが、表示されません。テキストは正常に表示されますが、行が表示されません。

編集:提案されているように動的に追加することと、コードを変更することの両方をテストしましたが、まだ行はありませんか? 何か不足していますか?

また、これは Fragment 内にあり、class extends Fragment {}

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); 
}
}
4

3 に答える 3

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);

このバージョンでは、システムがすべての作業を行うため、コードがよりクリーンになります。

于 2012-06-16T15:21:54.037 に答える
0

レイアウト パラメーターでは、高さを 1 に設定し、幅を親に合わせて設定する必要があります。高さ/幅のパラメーターも1つだけ設定しています。以下を試してください

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

また、RelativeLayout に LayoutParams を使用していますが、LinearLayout を使用しています。LinearLayouts は以下をサポートしません。

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

LinearLayout.LayoutParams を使用します。LinearLayout は、追加された順序でビューを水平または垂直に積み重ねます。

于 2012-06-16T15:27:32.130 に答える
0

私が思うに似たようなことをしましたが、簡単な方法は、コンポーネントのクラス、特に行区切りを付けたい onDraw() メソッドを上書きすることです。

あなたの目標は、各テキストビューの最後に黒い線を追加することだと思うので、次のことができます:

1) pngの黒線を作成

2) TextView を拡張して 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 を作成するたびに、行が動的に作成されます。

それが役に立てば幸い。

于 2012-06-16T15:40:42.233 に答える