zangxiaoxin 2013-08-13 05:43 采纳率: 0%
浏览 18691

使用LinearLayout.getChildAt(i)获取一个线性布局的view,并实现content中实现方法

1、定义接口content的方法,如ok,cancle;
2、在View的处理类myview中实现content的方法。
3、通过contently.getChildAt(i)的方法获得View v;
4、可以使用 content ct = (content)v;
5、使用ct.ok()和ct.cancel();
代码如下:
ll = (LinearLayout) findViewById(R.id.contently);
btncancle = (Button) findViewById(R.id.buttoncancel);
btnok = (Button) findViewById(R.id.buttonok);
View view = (View) ll.getChildAt(0);
Toast.makeText(getApplicationContext(),String.valueOf(view.getId()) , Toast.LENGTH_LONG).show();
final myview mv=(myview)view;//这里也无法运行
//content ct=(content)view;也不行
// Toast.makeText(getApplicationContext(),ct.getClass().toString() , Toast.LENGTH_LONG).show();
btncancel.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

// ct.cancel();
mv.cancel();
}
});
btnok.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

// ct.ok();
mv.ok();
}
});
}

public class myview extends View implements content{

    public myview(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    public myview(Context context,AttributeSet attr){
        super(context,attr); 
    }
    @Override
    public void cancle() {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(),"cancel" , Toast.LENGTH_LONG).show();
    }

    @Override
    public void ok() {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(),"ok", Toast.LENGTH_LONG).show();
    }

}
public interface content {
public void ok();
public void cancel();
}

  • 写回答

2条回答

  • qq_28066623 2016-10-17 09:06
    关注

    随便取个名字做一个 xml 文件 放在 layout 文件夹 也就是和 activity_main.xml 同级目录 把它们加上 外层 要加布局的 不加也可以 在java里addView 网上有的是

    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:textSize="50dp"
    android:textColor="#f00"
    android:id="@+id/text" />

    在你的 activity_main.xml 把 下面添加 上

    动态添加处 不是动态添加的也可以 只要你能明确的知道你 要操作第几行 或第几列

    android:animateLayoutChanges="true"
    android:id="@+id/tianjiachu"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="0px">

    </LinearLayout>
    

    按键 是button 还是 TextView 不是问题 问题是 android:onClick="anjian" android:id="@+id/xxx"

        <TextView
            android:onClick="anjian"
            android:id="@+id/xxx"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@color/baise"
            android:text="测试"
            android:textSize="20dp"/>
    
        <TextView
            android:onClick="anjian"
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:textColor="@color/baise"
            android:text="添加"
            android:textSize="20dp"/>
    

    在 java Activity 里 动态添加

    int a = 0;
    private void tianjia() {
    LinearLayout tianjiachu = (LinearLayout)findViewById(R.id.tianjiachu);
    View view = LayoutInflater.from(this).inflate(R.layout.你XML文件取得名字,null,false );
    TextView text = (TextView)view.findViewById(R.id.text);
    text.setText("ID"+a);
    a++;
    }

    public void anjian(View v) {
    switch (v.getId()) {
    case R.id.add: tianjia();
    break;
    case R.id.xxx:
    LinearLayout XX = (LinearLayout)findViewById(R.id.tianjiachu); //先找到 你要操作的 布局文件

    View cccccccccccc =XX.getChildAt(99999999999); //99999999999 你想改变 第几行 或者列 或者删除 随便你 不过你得有id ID相同不是问题

                                   TextView text = (TextView)cccccccccccc.findViewById(R.id.text);
    
    
                                    text.setText("行不行啊");
                break;
    

    }
    这应该是最简单的 动态添加操作了 也应该是 最简单的 编写了 至于怎么知道自己要操作 第几行 第几列 ?

    **我这个方法是用来 滑动分页 的 HorizontalScrollView + LinearLayout 实现滑动分页 **

    好了 不扯了 博主的 xxxxxxxxx.getChildAt(00000000000000000000000000000) 给了我最大的帮助 在此先谢过了

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算