gpt4 book ai didi

java - Android 2.3.3,在 View 中创建一行

转载 作者:行者123 更新时间:2023-12-01 15:20:03 25 4
gpt4 key购买 nike

尝试在我的 View 中创建一条黑线来分隔文本 block ,但它没有显示。文本按其应有的方式显示,但我没有看到该行。

编辑:已经测试过按照建议动态添加以及修改我的代码但仍然没有行?我错过了什么吗?

这也是在 Fragment 内部,类扩展 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);
}
}

最佳答案

您需要以编程方式创建 View 吗?

一个简单的方法是创建具有以下属性的 View

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

如果您创建一个名为 line.xml 的新 XML 文件,您可以使用 LayoutInflater动态获取 View :

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

此版本将生成更加简洁的代码,因为系统会为您完成所有工作。

关于java - Android 2.3.3,在 View 中创建一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11064569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com