Сегодня попробовал способ создания контролов из кода. Использовать этот метод в реальных приложениях не стоит, но для общего развития ознакомиться стоит.
Вот пример кода, который создает то же самое, что и main.xml по-умолчанию.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <span style="color: #ffcc00;">package org.example.designfromcode; import java.security.KeyStore.LoadStoreParameter; import android.app.Activity; import android.graphics.drawable.GradientDrawable.Orientation; import android.os.Bundle; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.LinearLayout.LayoutParams;</span> <span style="color: #ffcc00;">public class designfromcode extends Activity {</span> <span style="color: #ffcc00;">/** Called when the activity is first created. */</span> <span style="color: #ffcc00;">@Override public void onCreate(Bundle savedInstanceState) {</span> <p style="padding-left: 30px;"><span style="color: #ffcc00;">super.onCreate(savedInstanceState); LinearLayout.LayoutParams lp;</span></p> <p style="padding-left: 30px;"><span style="color: #ffcc00;">//устанавливаем параметры для корневого LinearLayout lp = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); LinearLayout.LayoutParams lpTextView;</span></p> <p style="padding-left: 30px;"><span style="color: #ffcc00;">//устанавливаем парамтры для TextView lpTextView = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);</span></p> <p style="padding-left: 30px;"><span style="color: #ffcc00;">//создаем корневой LinearLayout и устанавливаем для него orientation = vertical LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL);</span></p> <p style="padding-left: 30px;"><span style="color: #ffcc00;">//создаем TextView и устанавливаем текст TextView tv = new TextView(this); tv.setText(R.string.hello);</span></p> <p style="padding-left: 30px;"><span style="color: #ffcc00;">//добавляем TextView в LinearLayout ll.addView(tv,lpTextView);</span></p> <p style="padding-left: 30px;"><span style="color: #ffcc00;">//добавляем LinearLayout к контенту текущего Activity this.addContentView(ll, lp);</span></p> <p style="padding-left: 30px;"><span style="color: #ffcc00;"> }</span></p> <span style="color: #ffcc00;">}</span> |
<span style="color: #ffcc00;">package org.example.designfromcode; import java.security.KeyStore.LoadStoreParameter; import android.app.Activity; import android.graphics.drawable.GradientDrawable.Orientation; import android.os.Bundle; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.LinearLayout.LayoutParams;</span> <span style="color: #ffcc00;">public class designfromcode extends Activity {</span> <span style="color: #ffcc00;">/** Called when the activity is first created. */</span> <span style="color: #ffcc00;">@Override public void onCreate(Bundle savedInstanceState) {</span> <p style="padding-left: 30px;"><span style="color: #ffcc00;">super.onCreate(savedInstanceState); LinearLayout.LayoutParams lp;</span></p> <p style="padding-left: 30px;"><span style="color: #ffcc00;">//устанавливаем параметры для корневого LinearLayout lp = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); LinearLayout.LayoutParams lpTextView;</span></p> <p style="padding-left: 30px;"><span style="color: #ffcc00;">//устанавливаем парамтры для TextView lpTextView = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);</span></p> <p style="padding-left: 30px;"><span style="color: #ffcc00;">//создаем корневой LinearLayout и устанавливаем для него orientation = vertical LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL);</span></p> <p style="padding-left: 30px;"><span style="color: #ffcc00;">//создаем TextView и устанавливаем текст TextView tv = new TextView(this); tv.setText(R.string.hello);</span></p> <p style="padding-left: 30px;"><span style="color: #ffcc00;">//добавляем TextView в LinearLayout ll.addView(tv,lpTextView);</span></p> <p style="padding-left: 30px;"><span style="color: #ffcc00;">//добавляем LinearLayout к контенту текущего Activity this.addContentView(ll, lp);</span></p> <p style="padding-left: 30px;"><span style="color: #ffcc00;"> }</span></p> <span style="color: #ffcc00;">}</span>