android中的layout.requestfocus()(动态内容)

时间:2014-08-06 05:28:17

标签: android layout

我已经制作了一个应用程序,我将动态地将图像和文本添加到应用程序中,我正在使用

  

1.相对布局(主要布局)

     

1.(a)线性布局(包含所有1.(b)值的线性布局)

     

1.(b)线性布局(包含图像和文本的d布局),每个(1.(b))布局包含一个图像和一个文本

现在,因为我在机顶盒(带远程)上运行这个应用程序与android,它连接到智能电视,,,,,所以我必须使用机顶盒提供的遥控器控制我的应用程序。

所以我想要的是当应用程序加载时,焦点将放在第一个((1.(b)线性布局))上,即在第一对图像和文本上。

已经完成: - 我使用了requestFocus()但没有用

任何帮助将非常感谢提前欢呼!!!!!!!

   the code snippet is as follows:-
           activity
                         LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain1);
      RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                75, 98);
         params.addRule(RelativeLayout.CENTER_HORIZONTAL);
         params.setMargins(0, 25, 0, 0);
         RelativeLayout.LayoutParams paramrelative = new RelativeLayout.LayoutParams(
                170, 158);
         paramrelative.setMargins(2, 0, 0, 0); 
         RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(180,
                 ViewGroup.LayoutParams.WRAP_CONTENT);
         p.setMargins(2,0, 0, 0);

        //Create four 
       for(int j= 0;j<=count-1;j++) 
        {   
            // Create LinearLayout
           ScrollView sv = new ScrollView(this);
            final RelativeLayout ll = new RelativeLayout(this);

            ll.setBackgroundResource(R.drawable.app_bg);







            ll.setLayoutParams(paramrelative);
            ll.setFocusableInTouchMode(true);
            ll.setClickable(true);









        //    final String url= separated[j+1];


            // Create Button
            final  Button img = new Button(this);
            final TextView tv = new TextView(this);

                // Give button an ID
               img.setId(j+1);

               int s=img.getId();

            p.addRule(RelativeLayout.BELOW, s);

            tv.setLayoutParams(p);
            tv.setGravity(Gravity.CENTER);
              //File sdCardRoot = Environment.getExternalStorageDirectory();
                  // File sdcardPath = new File(Environment.getExternalStorageDirectory()
                    //      .getPath() + "/CATEGORIES");
               String filename=finalname[j];
               File myDir = new File(sdcardPath,filename);
            //     File myDir = new File(sdcardPath, "CATEGORIES_hotelinfo.png");
               String m1 = myDir.getPath();
               Resources res = getResources();
             Bitmap bitmap = BitmapFactory.decodeFile(m1);
               BitmapDrawable bd = new BitmapDrawable(res, bitmap);


              img.setBackgroundDrawable(Drawable.createFromPath(m1.toString()));

        //2ndaugust2014      tv.setText(finalname[j]);
            int s1= finalname[j].indexOf(".");
            final String finalname1=finalname[j].substring(0,s1);
            tv.setText(finalname1);



              img.setOnClickListener(new OnClickListener() {
                  public void onClick(View v) {
                        String newPath=newPath1+finalname1;

                      File sdcardPath = new File(Environment.getExternalStorageDirectory()
                            .getPath() +newPath); 
                      new CountDownTimer(2000,1000) {

                             public void onTick(long millisUntilFinished) {
                                   ll.setBackgroundResource(R.drawable.hover_btn_d);      //This is when you click on each tick it came here after 1000 millisecond
                             }

                             public void onFinish() {
                                 ll.setBackgroundResource(R.drawable.app_bg); 




                             }
                          }.start();
                      // if the directory does not exist, create it
                      if (!sdcardPath.exists())
                      {
                          Toast.makeText(getApplicationContext(), 
                                  "redirecting to www."+finalname1+".com", 
                                   Toast.LENGTH_SHORT).show();
                      }

                      else  



                      {  Intent intent = new Intent(Second60grid.this,Second60grid.class);
                  //  startActivity(intent);
                    //  i.setClassName(packageName, activitylast);

                      Toast.makeText(getApplicationContext(), 
                                   "name" + finalname1, 
                                    Toast.LENGTH_SHORT).show();





           intent.putExtra("nameoffile",newPath+"/");
              startActivity(intent);

                  }
                  }
              });

                // set the layoutParams on the button
               img.setLayoutParams(params);
               //Add button to LinearLayout
           //    setContentView(sv);
                ll.addView(img);
                ll.addView(tv);

            if(j==0)
                {
                  ll.requestFocus();
                }

               //Add button to LinearLayout defined in XML
                lm.addView(ll);  



        }

和xml文件如下:-----

                             <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
 android:background="@drawable/bg"> 
<RelativeLayout
      android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="200dip"
android:id="@+id/rl1"
 > 
<TextView
        android:id="@+id/TEXT0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#FF0000"
        android:text="CATEGORIES" />
 <LinearLayout
         android:id="@+id/linearMain1"
      android:layout_below="@+id/TEXT0"
       android:orientation="horizontal" 
     android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:background="#80000000">
           </LinearLayout>
            <LinearLayout
     android:id="@+id/linearMain2"
      android:layout_below="@+id/linearMain1"
       android:orientation="horizontal" 
           android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
             android:background="#80000000">
                       </LinearLayout>
                   <LinearLayout
                   android:layout_below="@+id/linearMain2"
                   android:orientation="horizontal" 
                  android:id="@+id/linearMain3"
                 android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
              android:background="#80000000"
                         >
                     </LinearLayout>  
                      <LinearLayout
                       android:layout_below="@+id/linearMain3"
                       android:orientation="horizontal" 
                 android:id="@+id/linearMain4"
                   android:layout_width="match_parent" 
                  android:layout_height="wrap_content" 
                android:background="#80000000"
               >  </LinearLayout> 
                  <LinearLayout
                android:layout_below="@+id/linearMain4"
                   android:orientation="horizontal" 
                android:id="@+id/linearMain5"
                android:layout_width="match_parent" 
               android:layout_height="wrap_content" 
                android:background="#80000000"
                 >
            </LinearLayout>
             </RelativeLayout>
            <RelativeLayout
                android:layout_width="1400dp"
             android:layout_height="70dp"
                android:background="#80ffffff" 
               android:layout_alignParentBottom="true"
               android:layout_below="@+id/rl1"
               android:id="@+id/rl2"
            >
         <ImageView
         android:id="@+id/remote_btn"
         android:layout_width="wrap_content"
          android:layout_height="wrap_content"
         android:adjustViewBounds="true"
         android:src="@drawable/remote_btn" 
         android:layout_alignParentBottom="true" />
        <TextView
        android:id="@+id/TEXT1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/remote_btn2"
        android:layout_toRightOf="@+id/remote_btn"
        android:text="MOVE" />

    <ImageView
        android:id="@+id/remote_btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="42dp"
        android:layout_toRightOf="@+id/TEXT1"
        android:adjustViewBounds="true"
        android:src="@drawable/remote_btn2" />
        <TextView
        android:id="@+id/TEXT2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TEXT1"
        android:layout_toRightOf="@+id/remote_btn2"
       android:text="SELECT" />
      </RelativeLayout>
      <RelativeLayout
      android:id="@+id/rl3"
      android:layout_width="wrap_content"
      android:layout_height="800dip"
      android:background="#10ffffff" 
      android:layout_marginLeft="10dip"
      android:layout_marginBottom = "10dp"
       android:layout_above="@+id/rl2"
         >
       <Button
      android:id="@+id/buttoniptv"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginTop="42dp"
      android:text="iptv" 
       android:background="#FF0000"/>
      <Button
      android:id="@+id/buttontvchannels"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"

      android:layout_below="@+id/buttoniptv"
      android:background="#FF0000"
      android:text="tvchannels" />
       <Button
      android:id="@+id/buttoninternet"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"

      android:layout_below="@+id/buttontvchannels"
      android:background="#FF0000"
      android:text="internet" />
   </RelativeLayout>
       </RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您目前正按升序添加视图

parentView.addView(child, 0); 

按降序添加:

parentView.addView(child, parentView.getChildCount() - 1);