【发布时间】:2014-10-21 09:42:54
【问题描述】:
我想以编程方式将一些 TextView 添加到 Vertical LinearLayout。 但有一个奇怪的问题: 当我为这些 TextViews 设置 Drawable(使用 setBackgroundDrawable() 方法)并以编程方式将它们添加到 LinearLayout 时,TextView 的 Drawable 无法正确显示。
只有当我有多个 TextView 时才会发生这种情况,前提是有一个 Background Drawable 正确显示。
只有当我以编程方式添加 TextView 时才会发生这种情况,如果我在 XML Drawable 中添加一些 TextView 就可以了。
我的代码:
rootView = inflater.inflate(R.layout.fragment, container, false);
linearLinear = (LinearLayout) rootView.findViewById(R.id.ll);
Drawable mDrawable =getActivity().getResources().getDrawable(R.drawable.textview_drawable);
TextView mTextView;
for(int i =0;i<textViewCount;i++)
{
mTextView = new TextView(context);
mTextView.setText(mText.get(i));
mTextView .setBackgroundDrawable(mDrawable);
linearLinear.addView(mTextView);
}
XML 可绘制:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#ffddf978" />
<corners
android:radius="9dp"
/>
<stroke
android:width="1dp"
android:color="#D7D7D7" />
</shape>
由于某种原因,第一个 TextView 的 Drawable 不正确,但第二个很好。 我从我的设备屏幕上拍摄了一些图像:
http://i61.tinypic.com/1zd5cu9.png
http://i58.tinypic.com/2nby79e.png
请帮助我!
抱歉英语不好。
【问题讨论】:
-
你也应该发帖
/res/drawable/textview_drawable.xml。 -
我做了,这是一个错误吗?或者我做错了什么
-
mTextView .setBackgroundDrawable(mDrawable);这是错字吗?删除空格。 -
尝试使用9个补丁图像而不是形状。
标签: android android-layout drawable