- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图将 Android XML 的外观与以编程方式创建它的方式关联起来,但我惨遭失败。我读过几个引用资料,指出 View 的 LayoutParams 类型必须等于父级的类型,但我只是不理解它。
给定以下 XML,我将如何以编程方式重新创建它?
<TableRow
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_gravity="center_vertical"
>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_gravity="center_vertical"
android:paddingTop="3dp"
android:orientation="vertical"
>
<EditText
android:padding="2dp"
android:layout_width="100dp"
android:layout_height="35dp"
android:layout_column="0"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:textSize="15dp"
android:numeric="integer"
/>
</LinearLayout>
<LinearLayout
android:layout_height="35dp"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:padding="2dp"
android:orientation="vertical"
>
<CheckBox android:text="Check1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="10sp"
android:background="@drawable/checkbox_background"
android:button="@drawable/checkbox"
android:textColor="@color/Gray"
/>
<CheckBox android:text="Check2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="10sp"
android:background="@drawable/checkbox_background"
android:button="@drawable/checkbox"
android:textColor="@color/Gray"
/>
</LinearLayout>
这是我尝试过的:
// Get the TableLayout
TableLayout tl = (TableLayout)findViewById(R.id.score_table);
LayoutParams lp;
TableRow.LayoutParams tp;
// Create a TableRow and give it an ID
TableRow tr = new TableRow(this);
tr.setId(100);
tr.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
LinearLayout ll0_1 = new LinearLayout(this);
// If the layout params of the child should be of
// the type of the parent this should be
// TableRow.LayoutParams?
tp = (TableRow.LayoutParams)tr.getLayoutParams();
tp.height = TableRow.LayoutParams.WRAP_CONTENT;
tp.width = TableRow.LayoutParams.FILL_PARENT;
tp.gravity = Gravity.CENTER_VERTICAL;
// TableRow.LayoutParams has no padding so this
// gets set on the LinearLayout?
ll0_1.setPadding(0,3,0,0);
ll0_1.setLayoutParams(tp);
tr.addView(ll0_1);
EditText et0 = new EditText(this);
et0.setId(100);
et0.setInputType(InputType.TYPE_CLASS_NUMBER);
// Same as above; LP set to type of parent
lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, 1f);
lp.height = 35;
lp.width = 100;
// Same as above; the parent's type doesn't have
// these attributes
et0.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
et0.setPadding(0, 3, 0, 0);
et0.setLayoutParams(lp);
ll0_1.addView(et0);
LinearLayout ll0_2 = new LinearLayout(this);
ll0_2.setOrientation(LinearLayout.VERTICAL);
tr.addView(ll0_2);
CheckBox cb0_1 = new CheckBox(this);
cb0_1.setTextSize(10f);
cb0_1.setTextColor(Color.GRAY);
cb0_1.setBackgroundResource(R.drawable.checkbox_background);
cb0_1.setButtonDrawable(R.drawable.checkbox);
cb0_1.setText("Nil");
ll0_2.addView(cb0_1);
CheckBox cb0_2 = new CheckBox(this);
cb0_2.setTextSize(10f);
cb0_2.setTextColor(Color.GRAY);
cb0_2.setBackgroundResource(R.drawable.checkbox_background);
cb0_2.setButtonDrawable(R.drawable.checkbox);
cb0_2.setText("10fer");
ll0_2.addView(cb0_2);
// Duplicate code removed for brevity
//...
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
没有错误,但与 XML 不匹配。相反,它看起来像这样:
第一个来自 XML,第二个是我尝试以编程方式复制它。
最佳答案
LayoutInflater 是最简单的方法:
public View myView(){
View v;
LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.myview, null);
TextView text1 = (TextView) v.findViewById(R.id.dolphinTitle);
Button btn1 = (Button) v.findViewById(R.id.dolphinMinusButton);
TextView text2 = (TextView) v.findViewById(R.id. dolphinValue);
Button btn2 = (Button) v.findViewById(R.id. dolphinPlusButton);
return v;
}
关于android - 程序化 Android 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7369635/
我是一名长期开发人员,但对 DNS 的经验并不丰富。这是我的问题: 我们的应用程序在 Amazon EC2 上为客户启动服务器。一个客户希望为每台启动的服务器使用自定义 DNS,而不是 AWS 提供的
我正在尝试以编程方式添加几个从 TextViews 扩展到 RelativeLayout 的 Tiles。 我的代码如下。 RelativeLayout.LayoutParams params = n
如果我在黑莓 (5.0.0) 上的电话应用程序中输入电话号码,我可以点击菜单按钮,其中一个选项是“添加到联系人”,这将显示添加联系人屏幕,电话号码已经人口稠密。我想在我的应用程序中做同样的事情 - 调
我试图将 Android XML 的外观与以编程方式创建它的方式关联起来,但我惨遭失败。我读过几个引用资料,指出 View 的 LayoutParams 类型必须等于父级的类型,但我只是不理解它。 给
我正在尝试创建 Joda 2.2 DateTimeFormatter 来重构我的代码。我试图重现的行为是 private String getSemesterLabel() { StringB
我几乎所有的用户界面都是以编程方式完成的,并在 Interface Builder 中进行了轻微的修改。但是 99% 的 UI 完全是用代码完成的,因为我觉得通过以下方式可以获得一定程度的灵 acti
我正在尝试学习如何以编程方式创建不同的 UI 元素。我的 UITableView 面临以下问题.. 我有 2 个 .swift 文件,一方面,我们有.. struct Settings
我想使用ONLY编程语法创建页面布局,但我无法将带有左、中、右内容 Pane 的边框容器插入到主 Pane 的顶部 Pane 中布局(底部布局也是如此)。 每次我尝试使用内容 Pane 作为父级时,都
我需要一些指示从哪里开始: 从任何使用 ALSA 播放音频的应用程序到连接的扬声器,我想获取样本并进行一些音频处理。我无法控制播放器,我希望能够处理来自任何来源的音频。基本上它将是一个 UV 计,也许
我想以编程方式(因为文件路径是在运行时生成的)为 SQL 语句创建一个单独的日志文件。我的代码看起来像这样 public static void InitializeLogger(FileInfo
如何以编程方式关闭嵌入式 jetty 服务器? 我这样启动 jetty 服务器: Server server = new Server(8090); ... server.start(); serve
我有一个派生自 QGraphicsView 的自定义类,它实现了一个插槽调用 scrollHorizontal(int dx),里面的代码很简单 void CustomView::scrollHo
我正在尝试了解在 ios 中布局 UI 的最佳实践。具体来说,我试图了解以下优点/缺点:纯编程 UI、 Storyboard和 XIB,以及 ios 6.0+ 中的自动布局。 我已经习惯了纯程序化 U
我正在尝试做类似于提出此问题的用户正在尝试做的事情: HTTPS Request From a Credential Provider DLL 关于用户个人资料的组成部分,我找到了this . Win
所以我试图为一些布局约束设置动画,但运气很差,我什么也没看到,然后我收到一条错误消息,说明它如何不能同时满足所有约束等。 我正在使用一个名为 PullableView 的类谁的动画用的是旧风格[UIV
我有大量的 Python 代码,需要对其施加符合 pep8 的样式/格式。 pep8 有各种规则的变体(如 this one by a coworker ),但最难的是处理分割太长的行和格式化修改后的
我试图让 ZeroClipboard API 工作,但看起来 setText() 函数没有效果。 提供简单示例 > here Copy to Clipboard ZeroClipboa
假设我有一个这样的错误应用程序: using System; namespace ConsoleApplication1 { class Program { stati
我是 Android 开发的新手,但遇到以下问题。我正在尝试以编程方式创建多个 ImageView 并使用 Picasso 加载图像,但我无意中发现了以下问题。 编辑:我想要实现的是我希望使用 wid
我需要以编程方式将 RDP 连接到虚拟机 (XP SP3/.NET3.5/VS 2008),(凭据已保存在 .rdp 文件中)并进行 UI 自动化测试。由于我们的域安全性,我需要以编程方式对交互式登录
我是一名优秀的程序员,十分优秀!