可以从活动文件中设置Linearlayout的背景



这是我的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);

是的,您可以通过

做到这一点
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"
    />

hmm也有linearlayout

的设置器
setBackgroundColor()

是从视图类继承的

就像您为按钮所做的那样,您也可以为线安排做同样的操作。给您linearlayout ID并设置颜色: -

((LinearLayout)findViewById(R.id.ll_test)).setBackgroundColor(Color.BLUE);

最新更新