簡體   English   中英

在線性布局中啟用水平滾動

[英]Enable horizontal scroll in linear layout

這是我沒有滾動的布局,外面是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該列表消失了。 請幫助我如何添加水平滾動。

<?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;
    }

}

HorizontalScrollView內部的LinearLayoutlayout_width應該為wrap_content

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM