【问题标题】:Writing custom LinearLayout for android为android编写自定义LinearLayout
【发布时间】:2010-07-26 19:26:38
【问题描述】:

我有一个自定义布局,在其子项下方绘制一个透明圆角矩形。问题是当我尝试将它添加到我的 xml 文件时,它没有显示出来。此外,当我尝试向其添加参数(即android:layout_width)时,弹出窗口显示它们都不可用。我添加的任何子视图都会发生同样的事情。有人可以帮忙吗?

public class RoundRectLayout extends LinearLayout 
{ 
 private RectF shape;
 public RoundRectLayout(Context context)
 {
  super(context);
  LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  layoutInflater.inflate(R.layout.settings, this);
  shape = new RectF();
 }

 public RoundRectLayout(Context context, AttributeSet attrs)
 {
  super(context, attrs);
  LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  layoutInflater.inflate(R.layout.settings, this);
  shape = new RectF();
 }

 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh)
 {
  shape = new RectF(0, 0, w - 5, h - 5);
  super.onSizeChanged(w, h, oldw, oldh);
 }

 @Override
 protected void dispatchDraw(Canvas canvas)
 {
  Paint temp = new Paint();
  temp.setAlpha(125);
  canvas.drawRoundRect(shape, 10f, 10f, temp);
  super.dispatchDraw(canvas);
 }
}

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    如果你只想为你的LinearLayout设置一个圆角矩形背景,你可以在“res/drawable/”的xml文件中定义一个可绘制的形状:

    <?xml version="1.0" encoding="UTF-8"?> 
    <shape xmlns:android="http://schemas.android.com/apk/res/android"> 
        <solid android:color="#50000000"/>    
        <stroke android:width="3dp"
                android:color="#ffffffff"/>
        <padding android:left="5dp"
                 android:top="5dp"
                 android:right="5dp"
                 android:bottom="5dp"/> 
        <corners android:bottomRightRadius="7dp"
                 android:bottomLeftRadius="7dp" 
                 android:topLeftRadius="7dp"
                 android:topRightRadius="7dp"/> 
    </shape>
    

    然后将布局的背景设置为可绘制 xml 文件的名称。如果您将其命名为round_border.xml,请将背景设置为:

    <LinearLayout
       android:background="@drawable/round_border"
       ...
    

    透明度设置如下...

    <solid android:color="#50000000"/>    
    

    前 2 位表示透明度,后 6 位表示颜色。

    【讨论】:

      【解决方案2】:

      这个教程真的帮我解决了你的问题

      首先,您必须在文件 res/values/attrs.xml 中定义要传递给自定义布局的字段

      <?xml version="1.0" encoding="utf-8"?>
      <resources>
           <declare-styleable name="MyLayout">
              <attr name="text" format="string" />
          </declare-styleable>
      </resources>
      

      现在准备你的构造函数来接收这些属性

      public RoundRectLayout(Context context, AttributeSet attrs) {
          super(context, attrs);
      
          TypedArray a = context.obtainStyledAttributes(attrs,
                  R.styleable.MyLayout);
          CharSequence s = a.getString(R.styleable.RoundRectLayout_text);
          if (s != null) {
              mText = s.toString();
          }
          a.recycle();
      }
      

      然后将命名空间添加到您的 layout.xml 以便 android konws 查找您的自定义布局。 “mi.pakage”是您自定义布局的 java 包。

      一旦你有了你的命名空间和你的类,只需调用你在 attrs.xml 中设置的属性

      <RelativeLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:myapp="http://schemas.android.com/apk/res/mi.pakage"
          android:id="@+id/root"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="#fff">
      
          <com.example.MyLayout
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content" 
              myapp:text="My Special Text String"/>
      
      </RelativeLayout>
      

      但是如果你只是想画一个矩形,你可以使用其他答案中建议的方法

      【讨论】:

        【解决方案3】:

        我认为是插件失败,但您可以指定 android:layout_*,它将被考虑在内。无论如何,如果您只想绘制圆形矩形布局,请按照 Marco 的说明进行

        【讨论】:

          猜你喜欢
          • 2013-05-20
          • 1970-01-01
          • 2012-08-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-09-06
          • 1970-01-01
          • 2020-11-21
          相关资源
          最近更新 更多