- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 android 应用程序中有以下模板。当我使用 android:layout_MarginTop="20dip"时效果很好,我可以在布局顶部看到 20dip 空间。
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dip"
style="@style/list_buttom_single"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<LinearLayout android:gravity="center_vertical" android:paddingLeft="10dip" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0">
<TextView android:text="Login / Register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/title" style="@style/content_page_large_text" />
<TextView android:text="sample text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/subtitle" android:visibility="visible" style="@style/content_page_small_text" />
</LinearLayout>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/itemCount" android:visibility="gone" style="@style/content_page_large_count_text" />
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/chevron" style="@style/list_buttom_chevron" />
</LinearLayout>
但是,当我尝试使用 LayoutInflater 以编程方式执行此操作时,我失去了这个 20dip 空间。
View notesView = mInflater.inflate(R.layout.list_item_single, null);
((TextView) notesView.findViewById(R.id.title)).setText("Notes");
((TextView) notesView.findViewById(R.id.subtitle)).setText("102 notes");
btnContainer.addView(notesView);
为什么我对“相同”操作有两种不同的行为,唯一的区别是一种是包含 uisng xml,另一种是使用 java.lang.
非常感谢
最佳答案
因为布局参数只有在指定父 View 的情况下才会与 View 相关联。这就是为什么你应该使用另一个版本的 inflate()
:
View notesView = mInflater.inflate(R.layout.list_item_single, btnContainer, false);
...
btnContainer.addView(notesView);
关于Android:为什么 layout_marginTop 只能与 include 标签一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6694412/
我使用的是相对布局,有 4 个按钮(在中间,一个在另一个下面)。 我的问题是调整所有按钮之间的间距,使所有按钮之间的间距相同。 我想根据屏幕的高度动态地执行此操作(我使用 Display 类来获取实际
我的 android 应用程序中有以下模板。当我使用 android:layout_MarginTop="20dip"时效果很好,我可以在布局顶部看到 20dip 空间。
在 Android 中,您能否创建一个监听器来捕捉 View 属性的变化(宽度/高度/边距/相对于屏幕顶部的位置)? 我想在 layout_marginTop="10dp"更改为不同的值时触发一个事件
我需要以编程方式创建一个使用 ImageView 和 TextView 的 Toast,它出现在屏幕中间,我已经做到了。 这就是我想要的渲染 (带心形的黑色方 block 是ImageView图片,“
因此,我允许用户在屏幕周围拖动包含在 ImageView 中的 RelativeLayout(以及其他一些内容)。该布局将 ImageView 包裹在其中,还有另一个相对布局将内容放在 ImageVi
我是一名优秀的程序员,十分优秀!