LineArLayout을 클릭하여 클릭하십시오
-
26-12-2019 - |
문제
이므로 LinearLayout을 클릭하여 LineArlayout을 사용하여 누르기를 클릭합니다.그리고 나는 거의 이렇게, 무언가가 추가 (여유 공간), 나는 아무것도 보지 못한다.
레이아웃이 추가되면 텍스트보기를 IT에 넣습니다.
my_layout.xml
<LinearLayout
android:id="@+id/layout_for_sides"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/title_side_size_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1 side size:" />
<EditText
android:id="@+id/inp_side_width_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="cm"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/inp_side_height_1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="cm"
android:inputType="number" >
</EditText>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btn_add_side"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add side" />
<Button
android:id="@+id/btn_calc_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Enter" />
</LinearLayout>
.
mainactivity.java
public class MainActivity extends Activity implements OnClickListener {
Button btn_add_side;
LinearLayout layout_for_sides;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout_for_sides = (LinearLayout) findViewById(R.id.layout_for_sides);
btn_add_side = (Button) findViewById(R.id.btn_add_side);
btn_add_side.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_add_side:
LinearLayout newlayout = new LinearLayout(this);
newlayout.setOrientation(LinearLayout.VERTICAL);
newlayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1));
newlayout.setBackgroundColor(Color.BLACK); //IT DOESN'T WORK, THERE IS NOTHING WITH BLACK BACKGROUND
layout_for_sides.addView(newlayout);
LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(this);
tv.setText("TextView");
tv.setId(5);
tv.setTextColor(Color.BLACK);
this.newlayout.addView(tv, lpView); //IT WORKS, I SEE FREE SPACE, BUT NEITHER TEXT
break;
default:
break;
}
}
}
.
질문은 (해결됨) : 아무것도 보지 못하는 이유는 무엇입니까?여유 공간 만 나타나지만 TextVeiw는 없습니다.
업데이트 : 새 질문 : 다른 linearlayout을 가진 layout_for_sides에 문제가 발생했습니다. 나는 layout_for_sides에서 모든 것을 제거하고 작동합니다. 그러나이 다른 레이어가 내 newlayout을 보여줄 것을 방지하는 방법을 이해하지 못합니다.
해결책 2
상위 LinearLayout의 잘못된 매개 변수의 문제점 - layout_for_sides.
Android : 방향="가로"가 있지만 Android가 필요합니다 : 방향="수직"이 필요합니다. 그 때문에 나는 아무것도 보지 못했다.
다른 팁
시도 :
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_add_side:
LinearLayout newlayout = new LinearLayout(this);
newlayout.setOrientation(LinearLayout.VERTICAL);
newlayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1));
//<---------------------Try this-------------------->
layout_for_sides.addView(newlayout,new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1));
LayoutParams lpView = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv = new TextView(this);
this.newlayout.addView(tv, lpView); //IT WORKS, I SEE FREE SPACE, BUT NEITHER TEXT
tv.setText("TextView");
tv.setId(5);
tv.setTextColor(Color.BLACK);
newlayout.setBackgroundColor(Color.BLACK); //IT DOESN'T WORK, THERE IS NOTHING WITH BLACK BACKGROUND
break;
default:
break;
}
}
. 제휴하지 않습니다 StackOverflow