在线性布局中启用水平滚动

时间:2014-10-07 18:29:50

标签: android

这是我没有滚动的布局,而且是http://i.stack.imgur.com/QQeXd.png。在" AlphebtesLinearLayout"我正在动态创建listView。还有一部分隐藏在右边,所以我需要去水平滚动。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
        <LinearLayout
            android:id="@+id/AlphebtesLinearLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >
        </LinearLayout>
</LinearLayout>

添加水平滚动后,我的输出为http://i.stack.imgur.com/ki86x.png,列表消失。 Plz帮我如何添加水平滚动。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


<HorizontalScrollView 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent">
        <LinearLayout
            android:id="@+id/AlphebtesLinearLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >
        </LinearLayout>

</HorizontalScrollView>
</LinearLayout>



protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tamil_alphabets);


    }
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        LinearLayout AlphebtesLinearLayout=(LinearLayout)findViewById(R.id.AlphebtesLinearLayout);
        int width=AlphebtesLinearLayout.getWidth();
        LinearLayout.LayoutParams para=new LinearLayout.LayoutParams(width/8,
                width/8 /*LinearLayout.LayoutParams.FILL_PARENT)*/);
        //list.setLayoutParams(para);
        createList(width);
    }
  public void createList(int width){
        map= new HashMap<String, String>();
        map2= new HashMap<String, String>();
        //--------------------------------------------
                final String[] Uirkeys = {"அ","ஆ","இ","ஈ","உ","ஊ","எ","ஏ","ஐ","ஒ","ஓ","ஔ","ஃ"};
                final String[] Uirvalues = {"a","aa","e","ee","ou","ou.","ay","ay.","ai","o","o.","ow","aak"};
            //----------------------------------------------------- 
                final String[] Uirkeys1={"க்","ங்","ச்","ஞ்","ட்","ண்","த்","ந்","ப்","ம்","ய்","ர்","ல்","வ்","ழ்",
                          "ள்","ற்","ன்"};
                final String[] Uirvalues1 = {"ik","ing","each","inj","it","in","ith","ind","ip","im","ye","er","il","ev","ill",
                           "ill.","er.","inn"};
            //------------------------------------------------------            
                final String[] Uirkeys2={"க","ங","ச","ஞ","ட","ண","த","ந","ப","ம","ய","ர","ல","வ","ழ",
                          "ள","ற","ன"}; 
                final String[] Uirvalues2 = {"ka","na.","sa","gna","ta","naa","tha","nha","pa","ma","ya",
                           "ra","la","va","la..","la.","ra","na"};
           //-------------------------------------------------------
            for(int loop=0;loop<15;loop++){ 
                if(count==0){
                    SelectKey=Uirkeys;
                    SelectValue=Uirvalues;
                }else if(count==1){
                    SelectKey=Uirkeys1;
                    SelectValue=Uirvalues1;
                }else if(count==2){
                    SelectKey=Uirkeys2;
                    SelectValue=Uirvalues2;
                }


                for(int i=0;i<SelectKey.length;i++){
                    map.put(SelectKey[i],SelectValue[i]);
                }
                for(int j=0;j<SelectKey.length;j++){
                    map2.put(SelectValue[j],SelectKey[j]);
                }
        LinearLayout.LayoutParams para=new LinearLayout.LayoutParams(width/8,
                LinearLayout.LayoutParams.FILL_PARENT);
        //para.weight=1;
        Activity activity = TamilAlphabets.this;
        TamilLetters=new TextView(TamilAlphabets.this);
        AlphebtesLinearLayout=(LinearLayout)findViewById(R.id.AlphebtesLinearLayout);
        //AlphebtesLinearLayout.setBackgroundColor(Color.RED);
        list=new ListView(this);
        list.setLayoutParams(para);
        GradientDrawable gd=new GradientDrawable();
        gd.setStroke(4, Color.BLACK);
        list.setBackgroundDrawable(gd);
        ListAdapterTamil LstAdapter=new ListAdapterTamil(TamilAlphabets.this,map,map2,SelectKey,SelectValue,activity);
        String s=map.get("a");
        list.setAdapter(LstAdapter);

        AlphebtesLinearLayout.addView(list);
        TamilLetters.setText("aa");
        TamilLetters.setLayoutParams(para);
        //TamilLetters.setText(map.get(Uirvalues[0]));
        //AlphebtesLinearLayout.addView(TamilLetters);
        //count++;
        }   
    }
}
class ListAdapterTamil extends BaseAdapter{
    Context context;
Map<String,String> map=new HashMap<String, String>();
Map<String,String> map2=new HashMap<String, String>();
private Activity activity;
String[] key;
String[] values;
    public  ListAdapterTamil(Context context,Map<String,String> map,Map<String,String> map2,String[] key,String[] values,Activity activity){
        this.context=context;
        this.map=map;
        this.map2=map2;
        this.key=key;
        this.values=values;
        this.activity=activity;
    }
    @Override

    public int getCount() {
        // TODO Auto-generated method stub
        return key.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.tamil_alphabets, parent, false);
        TextView txt=new TextView(activity);
        LinearLayout AlphebtesLinearLayout=(LinearLayout)rowView.findViewById(R.id.AlphebtesLinearLayout);
        AlphebtesLinearLayout.addView(txt);
        //txt.setText("ddd");
            //txt.setText(entry.getKey());
            txt.setText(map2.get(values[position]));
            txt.setPadding( 0, 0,0,50);
        return rowView;
    }

}

1 个答案:

答案 0 :(得分:0)

layout_width内的LinearLayout的{​​{1}}应为HorizontalScrollView