0

мне нужно что-то вроде этого:

LinearLayout (исправление)
- Кнопка
LinearLayout
- ScrollView
- TextView
- TextView
- TextView
Как добавить исправление Linear Layout программно в Android?

Я хочу, чтобы первый LinearLayout быть Исправить (например, заголовок). Когда кто-то пытается прокрутить вниз, я хочу, чтобы TextView позади исправления LinearLayout. Я хочу, чтобы эта кнопка всегда была видимой. С помощью этого кода все прокручивается вниз. Вот мой код:

ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView); 

    final LinearLayout linearLayout = new LinearLayout(this); 
    linearLayout.setOrientation(linearLayout.VERTICAL); 
    scrollView.addView(linearLayout); 

    final LinearLayout topLinearLayout = new LinearLayout(this); 
    topLinearLayout.setOrientation(topLinearLayout.VERTICAL); 
    linearLayout.addView(topLinearLayout); 

    final Button button = new Button(this); 
    button.setText(R.string.button); 
    topLinearLayout.addView(button); 

    TextView title = new TextView(this); 
    title.setText(R.string.title); 
    title.setGravity(Gravity.CENTER); 
    linearLayout.addView(title); 
+0

Вы можете создать его в качестве макета XML, а затем надуть его. С этим было бы намного легче справиться. 'LayoutInflater inflater = LayoutInflater.from (getContext()); View view = inflater.inflate (R.layout.yourLayout, yourContainer, attachToRootOrNot); ' – mgokgoz

+0

И как я могу это сделать? Можете быть более конкретными? –

+0

просто создайте макет для своего вида с помощью редактора макетов или вручную создайте xml, затем назовите его, как я написал. Чем вызывать вызовы детей, например, Button Button = view.findViewById (R.id.btn); 'Edit: вы можете проверить это видео моего приложения на betone.co.uk. Если один из экранов похож на то, чего вы хотите достичь, я могу поделиться с ним частью кода. – mgokgoz

ответ

0

Вы добавляете ваш topLinearLayout в ваш linearLayout, который вы добавляете в вашем scrollView. это не вы хотите.

добавить scrollView и topLinearLayout в другой LinearLayout

final LinearLayout mainlinearLayout = new LinearLayout(this); 
mainlinearLayout.setOrientation(linearLayout.VERTICAL); 

ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView); 

final LinearLayout topLinearLayout = new LinearLayout(this); 
topLinearLayout.setOrientation(topLinearLayout.VERTICAL); 

mainlinearLayout.addView(topLinearLayout); 
mainlinearLayout.addView(scrollView); 

final LinearLayout linearLayout = new LinearLayout(this); 
linearLayout.setOrientation(linearLayout.VERTICAL); 
scrollView.addView(linearLayout); 

final Button button = new Button(this); 
button.setText(R.string.button); 
topLinearLayout.addView(button); 

TextView title = new TextView(this); 
title.setText(R.string.title); 
title.setGravity(Gravity.CENTER); 
linearLayout.addView(title); 
+0

Спасибо, но это не работает. У меня есть эта ошибка: 'Невозможно запустить Activity ComponentInfo {com.example.alexm.iqtest/com.example.alexm.iqtest.IqTest}: java.lang.NullPointerException' –

+0

, в какой строке вы получаете эту ошибку? –

+0

Пожалуйста, возьмите журнал. Это все мои ошибки [log error] (http://www.gogo.ro/30.txt) –