Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/209.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 一种样式的一次充气,可多次使用_Android_Android Layout_Android Styles - Fatal编程技术网

Android 一种样式的一次充气,可多次使用

Android 一种样式的一次充气,可多次使用,android,android-layout,android-styles,Android,Android Layout,Android Styles,我想给一件物品充气一次,然后循环使用。我目前有一个解决方案,但很可能有更好的方法。此外,除非有view.removeView调用,否则程序不会运行,这是有道理的,但如果我以后想在应用程序中添加catBtn,则似乎很危险) 现行守则: LinearLayout col1 = (LinearLayout)findViewById(R.id.col1); for(int i = 0; i < 10; ++i) { LinearLayout assets = (LinearLayout)t

我想给一件物品充气一次,然后循环使用。我目前有一个解决方案,但很可能有更好的方法。此外,除非有
view.removeView
调用,否则程序不会运行,这是有道理的,但如果我以后想在应用程序中添加
catBtn
,则似乎很危险)

现行守则:

LinearLayout col1 = (LinearLayout)findViewById(R.id.col1);
for(int i = 0; i < 10; ++i) {
    LinearLayout assets = (LinearLayout)this.getLayoutInflater().inflate(R.layout.assets, null);
    Button btn = (Button)assets.findViewById(R.id.catBtn);//new Button(this);
    assets.removeView(btn);
col1.addView(btn);
}
LinearLayout col1=(LinearLayout)findViewById(R.id.col1);
对于(int i=0;i<10;++i){
LinearLayout assets=(LinearLayout)this.getLayoutInflater().inflate(R.layout.assets,null);
按钮btn=(按钮)资产。findViewById(R.id.catBtn);//新建按钮(此);
资产移除视图(btn);
col1.addView(btn);
}
现有资产


您可以将false作为最后一个参数传递给充气方法

LayoutInflator.from(context).inflate(res, parent, false);
这会导致膨胀视图不附着任何内容。这样你就不必移除任何东西。这就解决了assets.removeView()问题。但我认为这仍然可能是浪费

看起来您只需要一些按钮:

<Button android:id="@+id/catBtn" 
    android:layout_height="wrap_content" 
    android:background="@drawable/selectable" 
    android:text="Cat Button" 
    android:layout_width="120dip" 
    android:textSize="16dip">

让我们将其提取为一种样式:

<resources>
<declare-stylable android:name="awesome_button">
  <attr android:name="awesomeButtonStyle" android:type="reference"/>
</declare-stylable>

<style android:name="AwesomeButton">
 <item android:name="android:layout_height">wrap_content</item>
 <item android:name="android:background">@drawable/selectable</item>
 <item android:name="android:layout_width">120dp</item>
 <item android:name="android:text">Cat Button</item>
 <item android:name="android:textSize">16sp</item>
</style>

<style android:name="Theme.WithAwesomeButtons" parent="@android:style/Theme">
 <item android:name="awesomeButtonStyle">@style/AwesomeButton</item>
</style>


<resources>

包装内容
@可拉伸/可选择
120dp
猫按钮
16便士
@风格/魅力按钮
好了,现在我们开始时尚了;)(对不起,我无法抗拒)。现在,让我们在AndroidManifest.xml中配置您的活动:

<activity android:name=".MyCatBtnActivity"
 ... Whatever else is in your activity
 android:theme="@style/Theme.WithAwesomeButtons"/>

现在在您的循环中确定:

for (int i=0; i<10; i++) {
  // Let's get rid of the LayoutInflator (unless you want to use an xml layout
  // in which case, make awesomeButton.xml and have it just have a button in it
  // with attribute style="?awesomeButtonStyle").
  Button button = new Button(this, null, R.attr.awesome_button.awesomeButtonStyle));
  // Let's tag them with the integer counter so we can id them later
  // You can set id, but there is a slight chance it will not be unique 
  // within the hierarchy. Later on you can either use col1.getChildView(index) to scan
  // and look for these tags (or store them in a local array if col1 holds a lot of views)
  // Then you can also evaluate the tag whenever you are referring to a button from
  // within an OnClickListener or any View listener for that matter.
  button.setTag(Integer.valueOf(i));
  col1.add(button);
}

for(int i=0;i)您的示例不是开箱即用的,但让我有了一个良好的开端。我发布了一个链接,在接下来的过程中(作为编辑),但既然你的帖子90%都在那里,我说你得到了好评。不仅如此,你在答案中加入了一些实时性,我真的很感激你的长度,所以这肯定是a+1。谢谢!谢谢!很抱歉,我没有编译它或任何东西,所以我认为会有一些错误。很高兴你走完剩下的路:)
for (int i=0; i<10; i++) {
  // Let's get rid of the LayoutInflator (unless you want to use an xml layout
  // in which case, make awesomeButton.xml and have it just have a button in it
  // with attribute style="?awesomeButtonStyle").
  Button button = new Button(this, null, R.attr.awesome_button.awesomeButtonStyle));
  // Let's tag them with the integer counter so we can id them later
  // You can set id, but there is a slight chance it will not be unique 
  // within the hierarchy. Later on you can either use col1.getChildView(index) to scan
  // and look for these tags (or store them in a local array if col1 holds a lot of views)
  // Then you can also evaluate the tag whenever you are referring to a button from
  // within an OnClickListener or any View listener for that matter.
  button.setTag(Integer.valueOf(i));
  col1.add(button);
}