【问题标题】:How to set alpha value for my Relative layout?如何为我的相对布局设置 alpha 值?
【发布时间】:2012-07-04 20:46:15
【问题描述】:

您好,我正在尝试为我的相对布局设置 alpha 值,但是,我遇到了错误,如何解决这个问题帮助我.....

我的 xml 布局中有三个布局 第一个布局用于背景 第二个布局用于页眉 第三个布局用于页脚。我希望设置 alpha 值 2 和 3rd 布局所以我尝试了很多方法仍然不知道请告诉如何设置 alpha 值

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" 
    android:background="@drawable/blue">

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="60px"
        android:orientation="horizontal"
        android:layout_alignParentTop="true"
        android:background="@drawable/gradient_black"
        android:id="@+id/ttest">
       <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

        />

        <TextView
            android:id="@+id/header_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="6dp"
            android:layout_toRightOf="@+id/imageView1"
            android:text="settings"
            android:textColor="@color/white"
            android:textSize="20sp" />

    </RelativeLayout>

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="55px"

        android:background="@drawable/gradient_black"
        android:gravity="bottom"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true" >
  <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ssss"
        />  

    </RelativeLayout>

</RelativeLayout>

代码:

public class DesignActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        int width = getWindowManager().getDefaultDisplay().getWidth();
        int height = getWindowManager().getDefaultDisplay().getHeight();


        ImageView imgHead = (ImageView)findViewById(R.id.imageView1);

        ImageView imgbottom = (ImageView)findViewById(R.id.imageView2);


        imgbottom.setImageResource(R.drawable.back);
        imgbottom.setLayoutParams(new RelativeLayout.LayoutParams(width/8, height/8));


        imgHead.setImageResource(R.drawable.b);
        imgHead.setLayoutParams(new RelativeLayout.LayoutParams(width/8, height/8));
     //   RelativeLayout relative = (RelativeLayout)findViewById(R.id.ttest);

    }
}

【问题讨论】:

    标签: android layout alpha


    【解决方案1】:

    试试这个

    AlphaAnimation alpha = new AlphaAnimation(0.5F, 0.5F);
    alpha.setDuration(0); // Make animation instant
    alpha.setFillAfter(true); // Tell it to persist after the animation ends
    // And then on your layout
    yourLayout.startAnimation(alpha);
    

    【讨论】:

      【解决方案2】:

      相对布局 rl; ...
      rl.setAlpha(0.5F);

      <RelativeLayout
          android:id="@+id/rl"
          android:layout_width="200dp"
          android:layout_height="200dp"
          android:background="@drawable/bg"
          android:alpha="0.5">
      

      【讨论】:

      • rl.setAlpha 需要 API >= 11
      • 这使得整个布局变得透明,而不仅仅是背景图像,看起来。当我将它添加到根 RelativeLayout 时,我有一些视图元素,例如按钮和文本视图,它们也采用了这个 alpha 值。
      【解决方案3】:

      在这些情况下,我通常倾向于同时设置颜色和 alpha,所以我只使用 rl.setBackgroundColor(0xAACCCCCC);,其中 A 是 alpha 值,C 是颜色,采用十六进制格式。

      例如:rl.setBackgroundColor(0x88000000); 用于 0.5 透明黑色背景。

      或在 XML 中:android:background="#88000000"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多