【发布时间】:2016-03-07 08:20:17
【问题描述】:
我一直在尝试使用循环以编程方式膨胀按钮的线性布局,但它根本没有显示在 UI 中。但是,数组会被填充。
我的按钮 xml:
<Button xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/BlackKey">
</Button>
我的风格资源:
<style name="BlackKey">
<item name="android:layout_height">0dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_weight">2</item>
<item name="android:background">@color/black</item>
<item name="android:layout_margin">3dp</item>
</style>
我的初始化代码:
container = (FrameLayout) findViewById(R.id.mainframe);
public Button [] BLACKKEYS = new Button[5];
LinearLayout blackkeys = new LinearLayout(this);
blackkeys.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
blackkeys.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 8; i++){
Button newbutton = (Button) LayoutInflater.from(this).inflate(R.layout.blackkey, blackkeys, false);
blackkeys.addView(newbutton);
BLACKKEYS[i] = newbutton;
}
container.addView(blackkeys);
【问题讨论】:
标签: android view android-inflate