#android #layout #android-emulator
#Android #макет #android-эмулятор
Вопрос:
Я вижу в своем графическом макете то, что я хочу, центральную кнопку вверху. На моем эмуляторе моя кнопка находится в верхнем левом углу.
Мой XML-файл:
<?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:background="@drawable/mairie01"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal" >
<Button
android:id="@ id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/decourvrir1" />
<Button
android:id="@ id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/schedule" />
<Button
android:id="@ id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/decouvrir2" />
</LinearLayout>
Более того :
Когда я нажимаю на кнопку, невозможно использовать другой макет.
Графический макет в порядке, но на моем эмуляторе нет фона для linearlayout и кнопки.
Связанный XML-файл:
<?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:background="@drawable/mairie01">
<Button
android:id="@ id/manger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/eat" />"
<Button
android:id="@ id/dormir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@ id/autres"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Извините за мой плохой английский.
Ответ №1:
Я исправил свою проблему простой очисткой своего проекта.
Ответ №2:
Вы уверены, что устанавливаете правильное содержимое макета в своем Java-коде?
public class YourActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // are you using your main.xml ?
// or programmatically created layout like the following:
//
// LinearLayout m_layout = new LinearLayout(this);
// for(int i=0; i<3; i){
// Button m_btn = new Button(this);
// m_layout.addView(m_btn);
// }
//
// setContentView(this.m_layout);
//
// ...
}
// ...
// Your Activity Logic ...
// ...
}
если вы не установили ориентацию макета, ориентация LinearLayout по умолчанию — ГОРИЗОНТАЛЬНАЯ. Вы можете установить его программно или путем редактирования main.xml файл, подобный следующему:
// ...
this.m_layout.setOrientation(LinearLayout.VERTICAL);
// ...
или
<LinearLayout android:id = "@ id/m_layout" android:orientation = "vertical" ... />
Комментарии:
1. да, я установил правильный макет. Мой XML-файл, main.xml n [code] открытый класс main расширяет действие { /**, вызываемое при первом создании действия. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); [/code] Я попробую программно, но как получить доступ к кнопке (например, к setText) после моего цикла?
2. Для моей основной деятельности это нормально: LinearLayout m_layout = new LinearLayout(this); m_layout.addView(viePratique); m_layout.addView(decouvrir); m_layout.addView(agenda); m_layout.setBackgroundResource(R.drawable.mairie01); m_layout.setHorizontalGravity(1); setContentView(m_layout);