1

これは私のxmlコードです:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:background="@color/blue"
android:gravity="center">

アクティビティファイルから色を設定することはできますか?

たとえば、ボタンに対してこれを行うことができます。

ImageButton x = (ImageButton) this.findViewById(R.id.btn1);
x.setBackgroundColor(color);
4

3 に答える 3

2

はい、あなたはそれを行うことができます

public class MainActivity {
   private LinearLayout ll;

    @Override
    public void onCreate(...) {
      super.onCreate(...);
      setContentView(R.layout.your_layout_name);
      ll = (LinearLayout) findViewById(R.id.linear_layout_id);

      // You can set Background Color for your Linear Layout
      ll.setBackgroundColor(Color.RED);
      // You can set Image Also
      ll.setBackgroundResource(R.drawable.imagename);
 }
}

XMLファイルの場合:

    <LinearLayout 
            android:id="+@id/linear_layout_id"
            android:width = "fill_parent"
            android:height = "fill_parent"
            android:orientation = "vertical"
    />
于 2012-11-10T13:02:51.060 に答える
1

うーん、LinearLayoutのセッターもあります

setBackgroundColor()

Viewクラスから継承されます

于 2012-11-10T13:00:54.247 に答える
1

ボタンに対して行ったのと同じように、LinearLayoutに対しても同じことができます。LinearlayoutにIDを与え、色を設定します:-

((LinearLayout)findViewById(R.id.ll_test)).setBackgroundColor(Color.BLUE);
于 2012-11-10T13:13:57.100 に答える