2

Я работаю над пользовательским представлением. когда я ставлю пользовательский вид в горизонтальной прокрутки зрения горизонтальной прокрутке не working.I думаю, я сделал ошибку в написании MeasureSpec, но я не знаю, как написать MeasureSpec для пользовательского представленияПользовательский вид внутри Горизонтальная прокрутка без прокрутки

мой взгляд класс кода как это

public class Makecell extends ViewGroup{ 
    private int line_height; 
    private int line_width; 
    Context mContext; 
    subview sub; 
    int left,right,top,bottom; 
    MainActivity main; 
    public Makecell(Context context) { 
     super(context); 
     mContext=context; 
     // TODO Auto-generated constructor stub 
    } 

    public Makecell(Context context, AttributeSet attrs){ 
     super(context, attrs); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     // TODO Auto-generated method stub 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 

     final int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight(); 
      int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom(); 

      final int count = getChildCount(); 
      int line_height = 0; 
      int line_width=0; 

      int xpos = getPaddingLeft(); 
      int ypos = getPaddingTop(); 
      for (int i = 0; i < count; i++) { 
       final View child = getChildAt(i); 
       if (child.getVisibility() != GONE) { 

        child.measure(
          MeasureSpec.makeMeasureSpec(width, MeasureSpec.UNSPECIFIED), 
          MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST)); 

        final int childw = child.getMeasuredWidth(); 
        final int childh = child.getMeasuredHeight(); 
        line_height = Math.max(line_height, child.getMeasuredHeight()); 
        line_width=Math.max(line_width, child.getMeasuredWidth()); 
        if (xpos + childh > height) { 
         xpos = getPaddingLeft(); 
         ypos += line_width; 
        } 
        xpos += childw; 
       } 
      } 

      this.line_width=line_width; 


    } 

    @SuppressLint("DrawAllocation") 
    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     // TODO Auto-generated method stub 

      final int count = getChildCount(); 
      final int width = r - l; 
      final int hight = b - t; 
      int xpos = 0; 
      int ypos = 0; 

      for (int i = 0; i < count; i++) { 
       final View child = getChildAt(i); 
       if (child.getVisibility() != GONE) { 
        final int childw = child.getMeasuredWidth(); 
        final int childh = child.getMeasuredHeight(); 
//     
        if (xpos + childh > hight) { 
         xpos = getPaddingLeft(); 
         ypos += line_width; 


        } 
       child.layout(ypos, xpos,ypos + childw, xpos + childw); 
       xpos += childh; 

       } 
      } 
    } 
} 

и XML выглядеть следующим образом

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 
    <HorizontalScrollView 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:fillViewport="true" 
     android:id="@+id/rootlin" 
     > 
    </HorizontalScrollView> 
</RelativeLayout> 

добавив вид в коде выглядеть как

cell=new Makecell(getApplicationContext()); 
     mainlin=(HorizontalScrollView)findViewById(R.id.rootlin); 
     LinearLayout lin=new LinearLayout(this); 
     lin.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT)); 
     lin.setOrientation(LinearLayout.HORIZONTAL); 
     for(int i=0;i<100;i++) 
     { 
      text=new TextView(this); 
      text.setText("Text "+i); 
      text.setTextSize(15); 
      text.setBackgroundColor(Color.CYAN); 
      text.setWidth(screenwidth/2); 
      cell.addView(text); 
     } 
     lin.addView(cell); 
     mainlin.addView(lin); 

может ли один помочь мне найти меня, где я сделал ошибки

Спасибо'S в Advance ..

+0

Почему вы используете HorizontalScrollView? Вы можете использовать только LinearLayout и установить его прокрутку. –

+0

не могли бы вы рассказать мне, как я могу установить прокрутку в линейный макет –

ответ

0

Оба из них работали:

в Java:

LinearLayout lin=new LinearLayout(this); 
    lin.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.MATCH_PARENT)); 
    lin.setOrientation(LinearLayout.HORIZONTAL); 
    lin.setHorizontalScrollBarEnabled(true); 

и в xml файле:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="horizontal" 
     android:orientation="horizontal" 
     tools:context=".MainActivity" > 
    </LinearLayout> 
+1

Сделал модификацию, что вы говорите, но не сработал –

Смежные вопросы