【问题标题】:Android Design DependencyAndroid 设计依赖
【发布时间】:2012-08-29 05:37:38
【问题描述】:

我动态创建了 LinearLayout 并在该布局中设置了可绘制的背景

当 Layout 空闲时,它看起来不错

问题是当我点击用户名edittext时布局已经分裂

当我点击布局折叠的密码编辑文本时

如果可以解决请给点建议

可绘制:button_shape.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#ffffffff"/> 
   <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
</shape>

注意:

用户名和密码是分开的LinearLayout

登录和注册按钮是分开的LinearLayout;

动态线性布局代码

LinearLayout mainlayout = new LinearLayout(con);
        mainlayout.setOrientation(1);
        mainlayout.setBackgroundDrawable(drawable);

        for(int i=0;i<data.size();i++){
            TextView tv = new TextView(con);
            EditText et = new EditText(con);
            tv.setText(data.get(i));
            tv.setHeight(50);
            tv.setPadding(22, 0, 0, 0);
            tv.setTextColor(0xff000000);
            mainlayout.addView(tv);
            et.setHint("Tap To Type");
            mainlayout.addView(et);
        }

【问题讨论】:

  • 如果不包含动态创建LinearLayout并设置其可绘制背景的代码,您将很难获得帮助。

标签: android android-layout


【解决方案1】:

您已将背景应用到用户名和密码布局。以同样的方式尝试为editText 应用背景。如果您不这样做,那么editText 将使用其默认属性并在获得焦点时与其父级一起折叠。希望对您有所帮助。

编辑:我自己运行代码。它工作正常。

LinearLayout mainlayout = new LinearLayout(this);
    mainlayout.setOrientation(1);
    mainlayout.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_xml));

    for(int i=0;i<2;i++){
        TextView tv = new TextView(this);
        EditText et = new EditText(this);
        tv.setText(""+i);
        tv.setHeight(50);
        tv.setPadding(22, 0, 0, 0);
        tv.setTextColor(0xff000000);
        mainlayout.addView(tv);
        et.setHint("Tap To Type");
        et.setBackgroundDrawable(getResources().getDrawable(R.drawable.normal));
        mainlayout.addView(et);
    }

    addContentView(mainlayout, new LinearLayout.LayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)));

editText 可绘制

<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ffffffff"/> 

【讨论】:

  • 这是我的动态线性布局代码 LinearLayout mainlayout = new LinearLayout(con); mainlayout.setOrientation(1); mainlayout.setBackgroundDrawable(drawable); for(int i=0;i
  • 对于那个editText et,设置一个你为mainLayout设置的背景
  • LinearLayout mainlayout = new LinearLayout(con); mainlayout.setOrientation(1); mainlayout.setBackgroundDrawable(drawable); for(int i=0;i
  • 您是否将相同的可绘制对象设置为您用于 mainLayout 的 editText?
  • 尝试使用不同的、简单的不带任何角的可绘制对象(使用正方形),因为您的父视图有角背景,儿童内部的角背景可能会改变外观
猜你喜欢
  • 2019-12-28
  • 2019-11-12
  • 1970-01-01
  • 1970-01-01
  • 2013-04-25
  • 2011-05-08
  • 2011-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多